EventListener will not work on clones [duplicate]

2019-09-05 06:48发布

This question already has an answer here:

http://jsfiddle.net/1wqdmo4o/

var whichSelected = document.querySelectorAll(".selected");

for(var i = 0; i < whichSelected.length; i++) {

    var clone = whichSelected[i].cloneNode(false);
    clone.addEventListener("click", function() {createOutline(clone)});
    document.body.appendChild(clone);

}

I have no idea why the event listener will not work on the clones. Any ideas is appreciated!

标签: javascript
1条回答
爷的心禁止访问
2楼-- · 2019-09-05 07:35

You are setting the z-index of the copied node to -1, so when clicking, you click on body.

Also, the id is the same as the copied node, you might want to change this.

查看更多
登录 后发表回答