怎么创建元素并且添家事件

hhhct 2010-04-02 11:19:51
加入有一个 div
<div id="div"></div>

我想在这个div下添加3个div
第一个红色
第二个兰色
第3个黄色

第一个bing顶 function(){alert(1)}
第2个bing顶 function(){alert(2)}
第3个bing顶 function(){alert(3)}

这个该怎么写啊
...全文
104 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhhct 2010-04-07
  • 打赏
  • 举报
回复
werwerwer
mykelly6 2010-04-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hhhct 的回复:]
忘了提一个要求了
用jq
...........
[/Quote]
我服了,代码写出来了,自己都不会写成jQuery的。。
sohighthesky 2010-04-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sohighthesky 的回复:]
JScript code
$("div").css({color:"#FF0000"}).html("红色").appendTo("#div")
[/Quote]
$("<div></div>").css({color:"#FF0000"}).html("红色").appendTo("#div")
sohighthesky 2010-04-02
  • 打赏
  • 举报
回复
$("div").css({color:"#FF0000"}).html("红色").appendTo("#div")
hhhct 2010-04-02
  • 打赏
  • 举报
回复
忘了提一个要求了
用jq
...........
sohighthesky 2010-04-02
  • 打赏
  • 举报
回复
var div=document.getElementById("div");
var div1=document.createElement("div");
div1.innerHTML=" 红色";
div1.style.color="#FF0000";
div1.onclick=function(){alert(1)}
div.appendChild(div1);//另外两个相同
郭大侠_ 2010-04-02
  • 打赏
  • 举报
回复

var ct=document.getElementById('div');
var div1=document.createElement("div");
div1.setattribute("style","background-color:red;");
ct.appendChild(div1);


daxian520 2010-04-02
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>�ޱ����ĵ�</title>
</head>
<body>
<div id="div">
</div>
<script type="text/javascript">
var oDiv = document.getElementById("div");
oDiv.innerHTML = "<div>第一个红色</div><div>第二个兰色</div><div>第3个黄色</div>";
var oDivChilds = oDiv.getElementsByTagName("div");
oDivChilds[0].style.color = "red";
oDivChilds[1].style.color = "blue";
oDivChilds[2].style.color = "yellow";
oDivChilds[0].onclick = function(){
alert(1)
};
oDivChilds[1].onclick = function(){
alert(2)
};
oDivChilds[2].onclick = function(){
alert(3)
};

</script>
</body>
</html>
諾临風 2010-04-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mykelly6 的回复:]
引用 4 楼 hhhct 的回复:
忘了提一个要求了
用jq
...........

我服了,代码写出来了,自己都不会写成jQuery的。。
[/Quote]

呵。。。
passself 2010-04-02
  • 打赏
  • 举报
回复

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
#div{
width:30px;
height:10px;
border:1px solid yellow;
}
.new{
width:30px;
height:10px;
border:1px solid black;
margin-left:15px;
margin-top:2px;
}
#temp1{
background:red;
}
#temp2{
background:blue;
}
#temp3{
background:#FF0;
}
</style>
<script src="jquery-1.3.2.js"></script>
<script>
$(document).ready(function(){
$("#div").click(function(){
for(var i=1; i<4;i++){
$(this).after("<div class='new' id=temp"+i+">"+"</div>");
var id="#temp"+ i;
$(id).click(show);// 绑定show函数
}
});
function show(){
alert("aaaaaaa")
}
});
</script>
</head>

<body>
<div id="div"></div>
</body>
</html>


这样会好看些
vnetcbd 2010-04-02
  • 打赏
  • 举报
回复
<div id="div"></div>

$(function(){
$('#id').html('<div></div> <div></div> <div></div>');
$('#id').find('div').eq(0).css({width:100px,height:80px,background:red})
.live('click',function(){
alert(1);
});
$('#id').find('div').eq(1).css({width:100px,height:80px,background:blue})
.live('click',function(){
alert(2);
});
$('#id').find('div').eq(2).css({width:100px,height:80px,background:yellow})
.live('click',function(){
alert(2);
});

})
passself 2010-04-02
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
#div{
width:30px;
height:10px;
border:1px solid yellow;
}
.new{
width:30px;
height:10px;
border:1px solid black;
margin-left:15px;
margin-top:2px;
}
#temp1{
background:red;
}
#temp2{
background:blue;
}
#temp3{
background:#FF0;
}
</style>
<script src="jquery-1.3.2.js"></script>
<script>
$(document).ready(function(){
$("#div").live("click",function(){
for(var i=1; i<4;i++){
$(this).after("<div class='new' id=temp"+i+">"+"</div>");
var id="#temp"+ i;
$(""+id+"").click(function(){
alert("sss");
});
}
});
});
</script>
</head>

<body>
<div id="div"></div>
</body>
</html>

87,994

社区成员

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

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