Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 第一次单击后更改元素的onclick处理程序_Javascript_Jquery_Closures - Fatal编程技术网

Javascript 第一次单击后更改元素的onclick处理程序

Javascript 第一次单击后更改元素的onclick处理程序,javascript,jquery,closures,Javascript,Jquery,Closures,我试图在对dom元素进行第一次onclick之后交换onclick方法。我只是没有取得任何成功,这似乎是某种关闭的损失,或什么的。我可以使用jQuery。您有两种方法来实现这一点,这两种方法都是通过使用jQuery函数实现的: 1) 使用oneAPI方法-此方法仅适用一次。您将单击它一次,然后选择自己的第二个处理程序,第一个处理程序将不会再次启动 div.onclick = function(data, dom) { return function() {

我试图在对dom元素进行第一次onclick之后交换onclick方法。我只是没有取得任何成功,这似乎是某种关闭的损失,或什么的。我可以使用jQuery。

您有两种方法来实现这一点,这两种方法都是通过使用
jQuery
函数实现的:

1) 使用
one
API方法-此方法仅适用一次。您将单击它一次,然后选择自己的第二个处理程序,第一个处理程序将不会再次启动

   div.onclick = function(data, dom) {
        return function() {
           if (data.seenAlready == true) { // HACK
              $(this).children().toggle();
              return;
           }
           recursiveSearch(data, dom);

          // after this onclick, I want to assign it to a toggle like function. no clue how to do it.
       }
  }(child, mycontainer.appendChild(div));
这里是这个API的链接

2) 您可以选择以下方法来替换事件处理程序

$(myselector).one(function(){
    $(this).click(myotherhandler);
});
这将关闭第一个处理程序,并只触发第二个处理程序

检查此jsbin:


好的,我想我可能知道问题所在。这件事发生了多次,把我的状态搞砸了。我要试试这个jquery。如果我已经引用了我的dom元素,我该如何使用Jquery?请你详细说明你的问题。你已经引用了你的dom元素是什么意思。我想你需要这个
$(DomReference)
$(myselector).click(function(){
    $(this).off();
    $(this).click("secondhandler");
});