如何获取 JQuery.trigger('click');启动鼠标单击 [英] How to get JQuery.trigger('click'); to initiate a mouse click

查看:19
本文介绍了如何获取 JQuery.trigger('click');启动鼠标单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解如何使用 JQuery 模拟鼠标点击.有人可以告诉我我做错了什么.

I'm having a hard time understand how to simulate a mouse click using JQuery. Can someone please inform me as to what i'm doing wrong.

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');
});

演示:FIDDLE

当我点击按钮#foo 我想模拟点击#bar 但是当我尝试这样做时,什么也没有发生.我也试过 jQuery(document).ready(function(){...}) 但没有成功.

when I click on button #foo I want to simulate a click on #bar however when I attempt this, nothing happens. I also tried jQuery(document).ready(function(){...}) but without success.

推荐答案

你需要使用 jQuery('#bar')[0].click(); 模拟鼠标点击实际 DOM 元素(不是 jQuery 对象),而不是使用 .trigger() jQuery 方法.

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.

注意:DOM Level 2 .click() 不适用于 Safari 中的一些元素.您将需要使用一种解决方法.

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天全站免登陆