为什么jQuery无法触发锚标记的本机点击? [英] Why jQuery cannot trigger native click on an anchor tag?

查看:101
本文介绍了为什么jQuery无法触发锚标记的本机点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我发现当我点击其他元素时,jQuery无法触发锚标记上的本机点击事件,下面的示例将无效:

Recently I found jQuery cannot trigger the native click event on an anchor tag when I'm clicking on other elements, the example below won't work:

html

<a class="js-a1" href="new.html" target="_blank">this is a link</a>
<a class="js-a2" href="another.html" target="_blank">this is another link</a>

javascript

$('.js-a1').click(function () {
  $('.js-a2').click();
  return false;
});

这是 jsfiddle - 1 。点击第一个链接不会触发第二个链接上的原生点击。

And here is the jsfiddle - 1. Click on the first link won't trigger native click on the second one.

经过一些搜索,我找到了解决方案和解释。

After some searches, I found a solution and an explanation.

使用原生DOM元素。

$('.js-a1').click(function () {
  $('.js-a2').get(0).click();
  return false;
});

这是 jsfiddle - 2

我在上发现了一条帖子jQuery 触发事件处理程序。它告诉我:


.trigger()函数不能用于模仿本机浏览器事件,例如单击文件输入框或锚标记。这是因为,没有使用与这些事件对应的jQuery事件系统附加事件处理程序。

The .trigger() function cannot be used to mimic native browser events, such as clicking on a file input box or an anchor tag. This is because, there is no event handler attached using jQuery's event system that corresponds to these events.



问题



所以我的问题出现了:

Question

So here comes my question:

如何理解'没有使用jQuery的事件系统附加的事件处理程序这些事件'

为什么没有这样的相应事件处理程序?

Why is there not such corresponding event handler?

我更新了我的jsfiddles,似乎有类名的错误和错误。

I update my jsfiddles, it seems there's and error on the class name.

推荐答案


没有使用与这些事件对应的jQuery事件系统附加事件处理程序

there is no event handler attached using jQuery's event system that corresponds to these events

这意味着,在学习材料的这一点上,没有使用 .click(function(){} .bind将jQuery事件处理程序附加到这些元素。 ('click',function(){})等。

This means, at this point of the learning material, no jQuery event handlers has been attached to these elements using .click(function() {} or .bind('click', function () {}), etc.

无参数。 click()用于从jQuery的角度触发( .trigger('click'))一个click事件,它将执行所有单击jQuery使用 .click 注册的事件处理程序, .bind .on 等。此伪事件不会发送到浏览器。

The no-argument .click() is used to trigger (.trigger('click')) a "click" event from jQuery's perspective, which will execute all "click" event handlers registered by jQuery using .click, .bind, .on, etc. This pseudo event won't be sent to the browser.


.trigger()

执行附加到给定事件类型的匹配元素的所有处理程序和行为。

Execute all handlers and behaviors attached to the matched elements for the given event type.

检查 更新的jsFiddle示例 ,点击两个链接看到差异。希望它有所帮助。

Check the updated jsFiddle example, click on the two links to see the difference. Hope it helps.

这篇关于为什么jQuery无法触发锚标记的本机点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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