带有事件的javascript cloneNode

带有事件的javascript cloneNode,javascript,gmail,greasemonkey,Javascript,Gmail,Greasemonkey,我正在为gmail编写一个greasemonkey脚本,我需要复制一份“收件箱”链接。使用cloneNode可以正常工作,但我认为有一个onclick事件在运行时附加到它。这是一个由两部分组成的问题: 1.是否有办法查看哪些事件附加到节点? 2.有没有办法复制这些事件? 我找到的最接近jQuery的东西,我还没有准备好去那里。 谢谢 除非是使用元素上的onclick属性设置的 不可靠(您可以复制onclick属性,但该属性是否继续工作取决于它是否被使用以及它的作用) 您最好添加自己的单击处理程序

我正在为gmail编写一个greasemonkey脚本,我需要复制一份“收件箱”链接。使用cloneNode可以正常工作,但我认为有一个onclick事件在运行时附加到它。这是一个由两部分组成的问题: 1.是否有办法查看哪些事件附加到节点? 2.有没有办法复制这些事件? 我找到的最接近jQuery的东西,我还没有准备好去那里。 谢谢

  • 除非是使用元素上的
    onclick
    属性设置的
  • 不可靠(您可以复制
    onclick
    属性,但该属性是否继续工作取决于它是否被使用以及它的作用)
  • 您最好添加自己的
    单击
    处理程序,然后在原始服务器上触发该事件。。。或者以其他方式模拟行为

  • 除非是使用元素上的
    onclick
    属性设置的
  • 不可靠(您可以复制
    onclick
    属性,但该属性是否继续工作取决于它是否被使用以及它的作用)

  • 您最好添加自己的
    单击
    处理程序,然后在原始服务器上触发该事件。。。或者以其他方式模拟行为。

    我认为我们可以用这个理论来解决这个问题:

    我们有JS中的NodeList,也称为LiveList。我们可以随时检查它们的长度变化,将所需的公共事件添加到列表中的新元素(长度-1)


    我想我们可以用这个理论来解决这个问题:

    我们有JS中的NodeList,也称为LiveList。我们可以随时检查它们的长度变化,将所需的公共事件添加到列表中的新元素(长度-1)


    下面是使用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>
    
    
    //基于Selecor的实时列表[优势]
    var nodeList=document.getElementsByClassName('克隆')
    //节点列表的公共函数
    nodeList.addEvents=函数(){
    nodeList.item(nodeList.length-1).addEventListener('click',function()){
    console.log(this.id);
    });
    }
    nodeList.addEvents();
    //制作克隆
    var _clone=document.getElementsByTagName('div')[0].cloneNode(true);
    //更改Id
    _clone.id=“两个”
    document.body.appendChild(_clone);
    nodeList.addEvents();
    
    以下是使用节点列表添加事件的示例

    <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>
    
    
    //基于Selecor的实时列表[优势]
    var nodeList=document.getElementsByClassName('克隆')
    //节点列表的公共函数
    nodeList.addEvents=函数(){
    nodeList.item(nodeList.length-1).addEventListener('click',function()){
    console.log(this.id);
    });
    }
    nodeList.addEvents();
    //制作克隆
    var _clone=document.getElementsByTagName('div')[0].cloneNode(true);
    //更改Id
    _clone.id=“两个”
    document.body.appendChild(_clone);
    nodeList.addEvents();
    
    我也考虑过模拟点击事件,但我也找不到一种可靠的方法。这就是我想要的!我只需在原始节点上发送click。非常感谢你!我也考虑过模拟点击事件,但我也找不到一个可靠的方法来做。这就是我一直在寻找的!我只需在原始节点上发送click。非常感谢你!