Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.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 使用jQuery移动事件处理程序_Javascript_Jquery - Fatal编程技术网

Javascript 使用jQuery移动事件处理程序

Javascript 使用jQuery移动事件处理程序,javascript,jquery,Javascript,Jquery,我有一个让我发疯的问题,作为最后手段,我在这里提出一个问题 我想将onclick事件从一个元素移动到同一元素的onfocus事件,我使用以下代码: $("#theElement").bind("focus", {}, $("#theElement").data("events").click); $("#theElement").unbind("click"); 也许你已经猜到了。它不起作用 我该怎么做?在jquery 1.3.2的以下代码段中,我收到一条“fn.apply不是函数”消息: p

我有一个让我发疯的问题,作为最后手段,我在这里提出一个问题

我想将onclick事件从一个元素移动到同一元素的onfocus事件,我使用以下代码:

$("#theElement").bind("focus", {}, $("#theElement").data("events").click);
$("#theElement").unbind("click");
也许你已经猜到了。它不起作用

我该怎么做?在jquery 1.3.2的以下代码段中,我收到一条“fn.apply不是函数”消息:

proxy: function( fn, proxy ){
        proxy = proxy || function(){ return fn.apply(this, arguments); };
        // Set the guid of unique handler to the same of original handler, so it can be removed
        proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++;
        // So proxy can be declared as an argument
        return proxy;
    }
编辑:对不起,我应该提到。这来自一个插件,它在单击元素时执行一些操作。我想在元素获得焦点时做同样的事情,而不是在它被剪辑时,所以我想到的最简单的解决方案是这样的,因为我不能修改插件的代码

编辑:发现问题。这与@sje397在他的回答中发布的内容类似,而不是

$('#theElement').data('events').click[0]
但是


出于某种原因,即使只注册了一个事件,结果也是3(jquery代码中的guid)。

我发现jquery使用
.data()
将事件添加到元素中,因此您可以执行类似操作来检索事件处理程序:

$(el).data().events.click[0].handler

这不是一个优雅的解决方案,但如果您无法更改单击处理程序本身的任何内容,这将是唯一的解决方案。

我建议首先命名事件处理程序,如

$('#theElement').click(function myHandler() {
  //...
});
那么,你可以做什么

$("#theElement").bind("focus", {}, myHandler);
$("#theElement").unbind("click");
这将使它更具可读性,并修复错误

如果你不能,那么你可以:

// assuming yours is the first handler
var myHandler = $('#theElement').data('events').click[0]; 

$("#theElement").bind("focus", {}, myHandler);
$("#theElement").unbind("click");

同样,在jQuery1.4.3+中,键被更改为
\uuuuu事件\uuuu
。看。

我不是100%清楚你到底想做什么。是否要将焦点和单击事件绑定到同一元素

$("#theElement").bind("focus click", function(){
    //do stuff
    $(this).unbind('focus click');
});
编辑:根据您的澄清-您想在焦点上伪造“点击”事件

$("#theElement").bind("focus", function(){
    $(this).trigger('click');
});

它不起作用,因为
.click()
是一个将事件绑定到元素的函数。它不是事件处理程序本身。现在我来看看您可以做些什么来调用事件处理程序;)我不能那样做。这来自一个插件,它在单击元素时执行一些操作。我想在元素获得焦点时做同样的事情,而不是在它被裁剪时,所以我想到的最简单的解决方案是这样的。我愿意接受任何其他的方法。谢谢你的回答。@user9238833:我发现了一个重复的问题。请参阅注释中的链接。如果有多个处理程序,是否可以取消绑定()特定处理程序?是的,名称空间。绑定时使用event.yournamespacename作为事件$('#element').bind('focus.namespacename',function(){//dostuff})这不是他想要的。OP希望将单击事件替换为焦点事件。顺便说一句,你不需要用逗号来分隔事件。这并不能用焦点来代替点击事件,但值得一试。你需要在上面解开点击处理程序,否则你可能会启动函数两次…@calumbrodie:后续的焦点呢?如果我在onfocus触发并取消绑定onclick时调用onclick,那么在后续的焦点上调用什么呢?
$("#theElement").bind("focus", function(){
    $(this).trigger('click');
});