各位高手!js自动生成了几个div怎样让其各自绑定点击事件,怎样知道点击的是哪个div
<html><head><script>vararray=newArray("aaaa","bbbb","cccc","eeee");functioninit(){cre...
<html>
<head>
<script>
var array = new Array("aaaa", "bbbb", "cccc", "eeee");
function init(){
createNewNode();
}
window.onload = init;
function createNewNode(){
for(var i=0; i<5; i++){
var newDivNode = document.createElement("div");
document.body.appendChild(newDivNode);
newDivNode.setAttribute("id", "newId"+i);
var idd = newDivNode.getAttribute("id");
newDivNode.style.width = "50px";
newDivNode.style.height = "50px";
newDivNode.style.background = "rgba(150,50,32,1)";
newDivNode.style.float = "left";
newDivNode.style.position = "absolute";
newDivNode.style.left = (60 * i) + "px";
newDivNode.style.color = "white";
newDivNode.addEventListener("click", function(){
}, false);
}
}
</script>
</head>
<body>
</body>
</html>
也就是要实现点击不同的div去对应array数组中不同的数据! 展开
<head>
<script>
var array = new Array("aaaa", "bbbb", "cccc", "eeee");
function init(){
createNewNode();
}
window.onload = init;
function createNewNode(){
for(var i=0; i<5; i++){
var newDivNode = document.createElement("div");
document.body.appendChild(newDivNode);
newDivNode.setAttribute("id", "newId"+i);
var idd = newDivNode.getAttribute("id");
newDivNode.style.width = "50px";
newDivNode.style.height = "50px";
newDivNode.style.background = "rgba(150,50,32,1)";
newDivNode.style.float = "left";
newDivNode.style.position = "absolute";
newDivNode.style.left = (60 * i) + "px";
newDivNode.style.color = "white";
newDivNode.addEventListener("click", function(){
}, false);
}
}
</script>
</head>
<body>
</body>
</html>
也就是要实现点击不同的div去对应array数组中不同的数据! 展开
3个回答
展开全部
<html>
<head>
<script>
var array = new Array("aaaa", "bbbb", "cccc", "eeee");
function init(){
createNewNode();
}
window.onload = init;
function createNewNode(){
for(var i=0; i<5; i++){
var newDivNode = document.createElement("div");
document.body.appendChild(newDivNode);
newDivNode.setAttribute("id", "newId"+i);
var idd = newDivNode.getAttribute("id");
newDivNode.style.width = "50px";
newDivNode.style.height = "50px";
newDivNode.style.background = "rgba(150,50,32,1)";
newDivNode.style.float = "left";
newDivNode.style.position = "absolute";
newDivNode.style.left = (60 * i) + "px";
newDivNode.style.color = "white";
newDivNode.addEventListener("click", function(){
alert(this.getAttribute("id"));
}, false);
}
}
</script>
</head>
<body>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在事件中加入this参数,通过this获取
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询