javascript 你能绑定 .click() 以便它在 onclick 之前触发吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6694434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:33:36  来源:igfitidea点击:

can you bind .click() so that it fires prior to onclick?

javascriptjqueryjsfclickonclick

提问by DA.

I'm writing jQuery for an app that's being build in JSF. The JSF components are using a lot of their own JS for doing whatever JSF does and they use a lot of onclick attributes to handle it all.

我正在为在 JSF 中构建的应用程序编写 jQuery。JSF 组件使用大量自己的 JS 来完成 JSF 所做的任何事情,并且它们使用大量 onclick 属性来处理这一切。

Is there valid/proper way to bind your own click event to an element and ensure that your event fires prior to the default onclick event? It appears that by default a jQuery click function is fired after any inline onclick function calls.

是否有有效/正确的方法将您自己的点击事件绑定到元素并确保您的事件在默认的 onclick 事件之前触发?似乎默认情况下,在任何内联 onclick 函数调用后都会触发 jQuery click 函数。

回答by Alnitak

In jQuery events are triggered strictly in the order in which they were registered.

在 jQuery 中,事件严格按照它们注册的顺序触发。

You can circumvent any inline DOM0 onclickstyle handlers by iterating over the whole DOM, removing those onclickproperties, and then registering your own handler which theninvokes the originally defined function.

您可以onclick通过遍历整个 DOM,删除这些onclick属性,然后注册您自己的处理程序,然后调用最初定义的函数来绕过任何内联 DOM0样式处理程序。

Something like:

就像是:

$('[onclick]').each(function() {
    var handler = $(this).prop('onclick');
    $(this).removeProp('onclick');
    $(this).click(handler);
});

See http://jsfiddle.net/alnitak/2SCTK/

http://jsfiddle.net/alnitak/2SCTK/

So, register your own handlers first with jQuery, then invoke the code above to remove the original inline onclickhandlers and re-register them as if jQuery had added them.

因此,首先使用 jQuery 注册您自己的处理程序,然后调用上面的代码删除原始的内联onclick处理程序并重新注册它们,就好像 jQuery 添加了它们一样。

EDITcode simplified to just use the original function - jQuery will ensure that the function is invoked with the right context. Previous code which explicitly set the context to windowwas incorrect.

编辑代码简化为仅使用原始函数 - jQuery 将确保使用正确的上下文调用该函数。先前将上下文显式设置window为不正确的代码。

回答by Rocket Hazmat

You can remove the onClickevent from the DOM element, and then rebind it using jQuery.

您可以onClick从 DOM 元素中删除该事件,然后使用 jQuery 重新绑定它。

<div id="click" onclick="alert('DOM')">CLICK ME</div>

$('#click').click(function(){
    alert('jQuery');
});

var clickFunc = $('#click').prop('onclick');
$('#click').removeProp('onclick');
$('#click').click(clickFunc);

Events are called in the order they are bound, so the only way to call your function first, is to unbind, and then rebind the original function.

事件按照它们被绑定的顺序被调用,所以首先调用你的函数的唯一方法是解除绑定,然后重新绑定原始函数。

Demo: http://jsfiddle.net/wYd5t/2/

演示:http: //jsfiddle.net/wYd5t/2/

回答by Variant

There is no way to mandate event firing order for the same listener on the same element. Every browser is free to fire the events in any order it chooses.

无法为同一元素上的同一侦听器强制执行事件触发顺序。每个浏览器都可以按照它选择的任何顺序自由地触发事件。

回答by powtac

You could use onmousedownon the element.

您可以onmousedown在元素上使用。