div嵌入div的问题

imdthowru 2008-09-08 09:19:13
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript">
function newtag(){
var o = document.createElement("div");
document.getElementById("fuck").appendChild(o);
o.style.width = 200;
o.style.height = 200;
o.style.left = 100;
o.style.top = 100;
o.style.zIndex = 10000;
o.style.backgroundColor = "red";
o.onclick = alert("fuck");
}
</script>
</head>
<body>
<div id="fuck" style=" width:400px; height:400px; background-color: Black" >
</div>
<script type="text/javascript">
newtag();
</script>
</body>
</html>
上面是代码,我用javascript在div中嵌入了另一个div,不过产生了一些问题,问题1是新建的div的left和top都没定义成功,还是在左上角显示;问题2是onclick函数并不是在点击的时候发生的而是load页面时候弹出的。
...全文
163 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
西安风影 2008-09-09
  • 打赏
  • 举报
回复
少了position = "absolute";属性,onclick赋值应赋函数名,或者匿名函数
<script type="text/javascript"> 
function newtag(){
var o = document.createElement("div");
document.getElementById("fuck").appendChild(o);
o.style.position = "absolute";
o.style.width = 200;
o.style.height = 200;
o.style.left = 100;
o.style.top = 100;
o.style.zIndex = 10000;
o.style.backgroundColor = "red";
o.onclick = function(){alert("fuck")};
}
</script>
dujun3245350 2008-09-09
  • 打赏
  • 举报
回复
学习
zaokong 2008-09-09
  • 打赏
  • 举报
回复
o.style.position = "absolute";
o.onclick = new Function("alert(\"fuck\")");
cgisir 2008-09-09
  • 打赏
  • 举报
回复
style.width, style.left .... 后面是有单位的, 上面朋友说的 绝对定位也很重要否则 设置 top 或left没意义... 还有 onclick=function(){alert(111)}这样表现的...
帮你改了,供参考
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head>
<title>Untitled Page </title>
<script type="text/javascript">
function newtag(){
var o = document.createElement("div");
document.getElementById("fuck").appendChild(o);
o.parentNode.style.position = "relative";
o.style.position = "absolute";
o.style.width = 200 + 'px';
o.style.height = 200 + 'px';
o.style.left = 100 + 'px';
o.style.top = 100 + 'px';
o.style.zIndex = 10000;
o.style.backgroundColor = "red";
o.onclick = function(){
alert("fuck");
}
}
</script>
</head>
<body>
<div id="fuck" style=" width:400px; height:400px; background-color: Black" >
</div>
<script type="text/javascript">
newtag();
</script>
</body>
</html>
dln1234 2008-09-09
  • 打赏
  • 举报
回复
1、少了 o.style.position = "absolute";
2、定义 onclick 时,应该是 o.onclick = new Function("alert(\"fuck\")");
正确
cmaisyd 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sd5816690 的回复:]
1、少了 o.style.position = "absolute";
2、定义 onclick 时,应该是 o.onclick = new Function("alert(\"fuck\")");
[/Quote]
正解
leon28 2008-09-09
  • 打赏
  • 举报
回复
要把父DIV当作位置参照必须将他的position 设为 "absolute"
sd5816690 2008-09-08
  • 打赏
  • 举报
回复
1、少了 o.style.position = "absolute";
2、定义 onclick 时,应该是 o.onclick = new Function("alert(\"fuck\")");
sd5816690 2008-09-08
  • 打赏
  • 举报
回复

<script type="text/javascript">
function newtag(){
var o = document.createElement("div");
document.getElementById("fuck").appendChild(o);
o.style.position = "absolute";
o.style.width = 200;
o.style.height = 200;
o.style.left = 100;
o.style.top = 100;
o.style.zIndex = 10000;
o.style.backgroundColor = "red";
o.onclick = new Function("alert(\"fuck\")");
}
</script>

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧