无法以编程方式触发 jQuery 单击事件
如果我理解正确,要以编程方式触发附加到具有 my-button
css 类的对象的 jQuery 单击事件,您应该能够执行以下操作:
$('.my-button').click();
出于某种原因,此代码无法触发附加到元素的单击事件。代码的 $('.my-button')
部分正在运行并返回一个元素。我们知道事件处理程序附加到该元素,因为单击该元素确实会触发其事件处理程序的代码。该处理程序附有以下简单代码:
$('<a class="my-button"/>')
.click(function() { /* code here */ })
.appendTo(parent);
是否存在事件触发不起作用的条件?被访问的元素是通过 jQuery 小部件创建的,小部件代码通过跨域 JSONP 调用检索并通过 eval
运行(我怀疑的因素)。
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:
$('.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:
$('<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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
事实证明这是加载两个 jQuery 脚本的情况。通过 JSONP 检索的脚本包括 jQuery 的加载,并且该 jQuery 对象用于附加事件处理程序。与此同时,在我同事的网页中,他加载了自己的 jQuery。因此,第二个 jQuery 对象不知道第一个事件处理程序,因此无法以编程方式调用该处理程序。
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.
你应该使用:
You should use:
我不知道跨域 JSONP 是否与此有关,但是我必须说,以编程方式触发与 html 链接有关的选择器上的单击事件 (
...
) 不起作用。我怀疑这必须是某种浏览器策略,以阻止弹出窗口。考虑到浏览器具有跟踪和阻止弹出窗口的机制,并且大多数情况下允许用户在新链接出现之前授权单击操作。
如果您可以通过 jQuery 以编程方式单击链接、重定向、弹出窗口以及所有这些东西会更容易做到,因此这是不可能的。需要明确的是:
Link1
您无法触发该操作。Link2
您可以在此处触发 onclick,因为它不包含 href。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.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:
<a class='test' href='http://www.example.com'>Link1<a/>
you cannot trigger that.<a class='test2'>Link2</a>
you can trigger an onclick here, because it doesn't contain href.我遇到了同样的问题,并更改了将事件绑定到函数的方式修复了它,
然后根据 http://api.jquery.com/trigger/
I had the same problem and changing the way I bound the event to the function fixed it
then changed the binding according to the example in http://api.jquery.com/trigger/
奇怪的。您尝试过
.trigger('click')
吗?理论上,它们应该是相同的(现在查看 jQuery 代码来找出答案)。 编辑:看起来 .click() 只是 .trigger('click') 的代理,因此它可能没有帮助。为了进行调试,请尝试在页面上绑定实时点击事件小部件被加载到。
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.For debugging, try to bind a live click event on the page that the widget is loaded in to.
我最好的猜测是处理程序在您尝试触发事件后被绑定。
尝试:
或使用您的原始代码 - 在 JSONP 请求的回调中触发事件。
My best guess is the handler is bound after you are trying to trigger the event.
Try:
or using your original code - trigger the event in the callback of your JSONP request.
如果它没有回忆起正确记录的事件,尤其是自定义事件,则在您分配事件处理程序后,jQuery 库可能会再次加载。
使用以下命令检查您的代码:
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.
Check your code using:
我也遇到过这个问题,但我没有多个 jQuery。
阅读这里的答案我意识到这是因为执行顺序。在获得调用单击的函数后几行,我绑定了事件处理程序。
这是卫生部!片刻...
I also ran into this but I did not have multiple jQuery.
Reading through the answers here I realized it was because of the order of execution. I was binding the event handler a few lines after I had the function that invoked the click.
It was a DOH! moment...
返回 dom 并执行以下操作:
$("abutton")[0].click();
Go back to the dom and do it with:
$("abutton")[0].click();