我正在为gmail编写一个greasemonkey脚本,其中我需要制作一个“收件箱”链接的副本。使用cloneNode可以正常工作,但我认为在运行时会附加一个onclick事件。因此,这是一个由两部分组成的问题: 1.有没有办法查看节点上附加了哪些事件? 2.有没有办法也复制这些事件?我找到的最近的东西是jQuery,但我还没有准备好去那里。谢谢!
发布于 2009-05-02 21:29:42
onclick属性设置的。onclick属性,但这是否会继续工作取决于它是否被使用以及它做了什么)。您最好添加自己的click处理程序,然后在原始的...或者以其他方式模拟行为。
发布于 2013-08-11 02:20:03
我认为我们可以用这个理论来解决这个问题:
我们在JS中有NodeList,也称为liveLists。我们可以在它们的长度发生变化时进行检查,将所需的公共事件添加到列表中的新元素中( addEvent -1)。
怎么说..。
发布于 2013-08-11 03:02:06
下面是使用Nodelist添加事件的示例。
<body>
<div id="one" class="clones" style="background:red;width:100px;height:100px"></div>
</body>
<script>
//Selecor based live list [Advantage]
var nodeList = document.getElementsByClassName('clones')
//Common Function for nodelist
nodeList.addEvents = function(){
nodeList.item(nodeList.length-1).addEventListener('click',function(){
console.log(this.id);
});
}
nodeList.addEvents();
//Making Clone
var _clone = document.getElementsByTagName('div')[0].cloneNode(true);
//Changing Id
_clone.id="two"
document.body.appendChild(_clone);
nodeList.addEvents();
</script>https://stackoverflow.com/questions/815642
复制相似问题