Javascript 无法以编程方式触发 jQuery 单击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4511874/
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-08-23 12:43:46  来源:igfitidea点击:

Cannot programmatically trigger jQuery click event

javascriptcross-domainevaljquery

提问by Jacob

If I understand correctly, to programmatically trigger a jQuery click event attached to an object with a css class of my-button, you should be able to just do this:

如果我理解正确,要以编程方式触发附加到具有 css 类的对象的 jQuery 单击事件my-button,您应该能够这样做:

$('.my-button').click();

For some reason, this code is failing to trigger the click event attached to the element. The $('.my-button')part of the code is working and returning one element. We know the event handler is attached to that element because clicking on the element does trigger its event handler's code. The handler is attached with the following simple code:

出于某种原因,此代码无法触发附加到元素的点击事件。$('.my-button')代码的一部分正在工作并返回一个元素。我们知道事件处理程序附加到该元素上,因为单击该元素确实会触发其事件处理程序的代码。处理程序附有以下简单代码:

$('<a class="my-button"/>')
    .click(function() { /* code here */ })
    .appendTo(parent);

Are there any conditions where event triggering does not work? The element being accessed is created through a jQuery widget, the widget code is retrieved through a cross-domain JSONP call and run through eval(the factor I suspect).

是否存在事件触发不起作用的情况?正在访问的元素是通过 jQuery 小部件创建的,小部件代码通过跨域 JSONP 调用检索并运行eval(我怀疑的因素)。

采纳答案by Jacob

This turned out to be a case of two jQuery scripts being loaded. The script retrieved via JSONP included the loading of jQuery, and that jQuery object was used to attach the event handler. Meanwhile, in my co-worker's web page, he had loaded his own jQuery. Therefore, this second jQuery object, having no knowledge of the first's event handlers, was unable to programmatically invoke the handler.

结果证明这是加载了两个 jQuery 脚本的情况。通过 JSONP 检索的脚本包括 jQuery 的加载,并且该 jQuery 对象用于附加事件处理程序。同时,在我同事的网页中,他加载了自己的 jQuery。因此,第二个 jQuery 对象不知道第一个的事件处理程序,无法以编程方式调用处理程序。

回答by Omkar

You should use:

你应该使用:

$('.my-button').trigger("click");

回答by tzik

I don't know if cross-domain JSONP has something to do with it, however I must say that programmatically triggering a click event on a selector that has to do with an html link (<a href='...'>...</a>) doesn't work.

我不知道跨域 JSONP 是否与它有关,但是我必须说以编程方式在与 html 链接 ( <a href='...'>...</a>) 相关的选择器上触发点击事件是行不通的。

I suspect it has to be some sort of browser policy, so as to block pop ups. Consider the fact that browsers, have a mechanism to track and block pop ups and mostly allow the user authorize a click action before the new link appears.

我怀疑它必须是某种浏览器策略,以便阻止弹出窗口。考虑到这样一个事实,即浏览器具有跟踪和阻止弹出窗口的机制,并且主要允许用户在新链接出现之前授权点击操作。

If you could programmatically click a link via jQuery, redirection, popups and all that stuff would be easier to do, hence it's not possible. Just to be clear:

如果您可以通过 jQuery 以编程方式单击链接、重定向、弹出窗口和所有这些东西会更容易做到,因此这是不可能的。只是要清楚:

<a class='test' href='http://www.example.com'>Link1<a/>you cannot trigger that.

<a class='test' href='http://www.example.com'>Link1<a/>你不能触发那个。

<a class='test2'>Link2</a>you can trigger an onclick here, because it doesn't contain href.

<a class='test2'>Link2</a>您可以在此处触发 onclick,因为它不包含 href。

回答by Davide

I had the same problem and changing the way I bound the event to the function fixed it

我遇到了同样的问题并改变了我将事件绑定到函数的方式修复了它

  var f=function() { .... }
  $('input.radioDomande').click(f);
  ...
  $(s+data.domanda[i].valore).trigger("click");
  //WRONG: It won't trigger the event

then changed the binding according to the example in http://api.jquery.com/trigger/

然后根据http://api.jquery.com/trigger/ 中的示例更改绑定

  var f=function() { .... }
  $('input.radioDomande').bind('click', f);
  ...
  $(s+data.domanda[i].valore).trigger("click");
  //IT DOES TRIGGER THE EVENT

回答by John Strickler

My best guess is the handler is bound after you are trying to trigger the event.

我最好的猜测是在您尝试触发事件后绑定了处理程序。

Try:

尝试:

var myButtons = $('<a class="my-button"/>)
                    .click(function() { /* code here */ })
                    .appendTo(parent);

myButtons.click();

or using your original code - trigger the event in the callback of your JSONP request.

或使用您的原始代码 - 在 JSONP 请求的回调中触发事件。

回答by simshaun

Odd. Have you tried .trigger('click')? Theoretically, they should be the same (looking into jQuery code right now to find out). Edit: It appears .click() is simply a proxy for .trigger('click'), so it probably won't help.

奇怪的。你试过.trigger('click')吗?从理论上讲,它们应该是相同的(立即查看 jQuery 代码以找出答案)。 编辑:看起来 .click() 只是 .trigger('click') 的代理,所以它可能无济于事。

For debugging, try to bind a live click event on the page that the widget is loaded in to.

为了调试,尝试在加载小部件的页面上绑定实时点击事件。

回答by John Smith

If it does not recall the events, especially the custom, which are properly recorded, it is likely that the jQuery library is loaded again, after you assign an event handler.

如果它不记得正确记录的事件,尤其是自定义事件,则可能是在分配事件处理程序后再次加载了 jQuery 库。

Check your code using:

使用以下方法检查您的代码:

jQuery (document).ready (function () {
    // Test your code here
});

回答by technocrusaders.com

Go back to the dom and do it with:

返回 dom 并执行以下操作:

$("abutton")[0].click();

$("abutton")[0].click();