jQuery.trigger('click')不起作用 [英] jQuery.trigger('click') not working

查看:114
本文介绍了jQuery.trigger('click')不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我无法正确理解 jQuery触发器('click')函数。

有人可以告诉我为什么这个简单的代码不工作,如何解决?



HTML:

 < a id =barhref =http://stackoverflow.comtarget =_ blank>不要点击我!< / a> 
< span id =foo>点击我!< / span>

jQuery:

 < $('#bar')。trigger('click'); 
}); code> jQuery('#foo')。

演示: FIDDLE



我想要的是可以点击#foo(它运行良好)并在#bar上模拟点击(完全忽略而没有任何错误消息)。
也试过了 jQuery(document).ready(function(){...})但没有成功。
<您需要使用 jQuery('#bar')[0] .click(); 解决方案

href =https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.click =noreferrer>模拟鼠标点击实际的DOM元素(不是jQuery对象),而不是使用 .trigger() jQuery方法。注意:DOM Level 2 .click()不适用于 Safari中的一些元素。您需要使用解决方法。



http:/ /api.jquery.com/click/


It seems that I just can not understand the jQuery trigger('click') function correctly.

Can someone please tell me why this simple code doesn't work and how to fix?

HTML:

<a id="bar" href="http://stackoverflow.com" target="_blank">Don't click me!</a>
<span id="foo">Click me!</span>

jQuery:

jQuery('#foo').on('click', function(){
    jQuery('#bar').trigger('click');
});

Demo: FIDDLE

What I want is to be able to click on #foo (which is working well) and simulating a click on #bar (which is completly ignored without any error messages). Tried also with the jQuery(document).ready(function(){...}) but without success.

解决方案

You need to use jQuery('#bar')[0].click(); to simulate a mouse click on the actual DOM element (not the jQuery object), instead of using the .trigger() jQuery method.

Note: DOM Level 2 .click() doesn't work on some elements in Safari. You will need to use a workaround.

http://api.jquery.com/click/

这篇关于jQuery.trigger('click')不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆