jQuery:如何触发锚链接的点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6068266/
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
jQuery: how to trigger anchor link's click event
提问by Venkat Papana
I have a anchor link like
我有一个锚链接
<a id="myanchor" href="http://google.com" target="_blank">Google</a>
How to open href target in a new tab programatically?
如何以编程方式在新选项卡中打开 href 目标?
回答by Igor G.
Try the following:
请尝试以下操作:
$("#myanchor")[0].click()
$("#myanchor")[0].click()
As simple as that.
就如此容易。
回答by GvS
There's a difference in invoking the clickevent (does not do the redirect), and navigating to the hreflocation.
调用click事件(不进行重定向)和导航到该href位置是有区别的。
Navigate:
导航:
window.location = $('#myanchor').attr('href');
Open in new tab or window:
在新标签页或窗口中打开:
window.open($('#myanchor').attr('href'));
invoke click event (call the javascript):
调用点击事件(调用 javascript):
$('#myanchor').click();
回答by Travis Kaufman
Even though this post is caput, I think it's an excellent demonstration of some walls that one can run into with jQuery, i.e. thinking click()actually clicks on an element, rather than just sending a click event bubbling up through the DOM. Let's say you actually needto simulate a click event (i.e. for testing purposes, etc.) If that's the case, provided that you're using a modern browser you can just use HTMLElement.prototype.click(see here for method details as well as a link to the W3 spec). This should work on almost all browsers, especially if you're dealing with links, and you can fall back to window.openpretty easily if you need to:
尽管这篇文章是有章可循的,但我认为它很好地展示了 jQuery 可能遇到的一些障碍,即思考click()实际上是点击了一个元素,而不是仅仅发送一个通过 DOM 冒泡的点击事件。假设您实际上需要模拟点击事件(即出于测试目的等)如果是这种情况,前提是您使用的是可以直接使用的现代浏览器HTMLElement.prototype.click(有关方法详细信息以及指向W3 规范)。这应该适用于几乎所有浏览器,尤其是在您处理链接时,window.open如果需要,您可以很容易地回退到:
var clickLink = function(linkEl) {
if (HTMLElement.prototype.click) {
// You'll want to create a new element so you don't alter the page element's
// attributes, unless of course the target attr is already _blank
// or you don't need to alter anything
var linkElCopy = $.extend(true, Object.create(linkEl), linkEl);
$(linkElCopy).attr('target', '_blank');
linkElCopy.click();
} else {
// As Daniel Doezema had said
window.open($(linkEl).attr('href'));
}
};
回答by Vinod Poorma
It worked for me:
它对我有用:
window.location = $('#myanchor').attr('href');
回答by Jobelle
window.open($('#myanchor').attr('href'));
$('#myanchor')[0].click();
回答by arun vats
$(":button").click(function () {
$("#anchor_google")[0].click();
});
- First, find the button by type(using ":") if id is not given.
- Second,find the anchor tag by id or in some other tag like div and $("#anchor_google")[0] returns the DOM object.
- 首先,如果没有给出 id,则按类型(使用“:”)查找按钮。
- 其次,通过 id 或其他一些标签(如 div 和 $("#anchor_google")[0] 返回 DOM 对象)找到锚标记。

