如何触发HTML链接(锚元素)的默认动作/事件? [英] How to trigger the default action/event of a HTML link (anchor element)?

查看:116
本文介绍了如何触发HTML链接(锚元素)的默认动作/事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何触发HTML链接(锚点元素)的默认动作/事件?这就是使用JavaScript / jQuery来点击现有的HTML链接,就好像用户点击了它。

How could one trigger the default action/event of a HTML link (anchor element)? That is to use JavaScript/jQuery to "click" an existing HTML link, as if the user has clicked it.

只需使用 .click( )似乎无效。

$('#alink').click();
// the nothing happening

对于这个HTML:

<a id="alink" href="http://google.com" target="_blank">a link</a>

示例小提琴: http://jsfiddle.net/dCfD8/

我宁愿不用JavaScript创建一个新窗口(并且照顾任何当点击一个链接时,还需要处理)。

I'd rather not create a new window in JavaScript (and take care of whatever else needs to be handled when a link is clicked).

推荐答案

您可以使用简单的<$ c $触发点击事件c>触发方法在jQuery中。

You can trigger the click event using a simple trigger method in jQuery.

$('#alink').trigger('click');

请注意,即使事件被触发,浏览器也不会跟踪链接href。遵循href的唯一方法是用鼠标自己实际点击它。

Beware though, that even in the event gets fired, the browser will not follow the link href. The only way to follow the href is to actually click it with the mouse yourself.

据我所知,没有办法强制链接的行为如同它被点击。您必须更改文档位置或类似的内容才能在页面之间实际导航。

As far as I know, there is no way to force a link to behave as if it were clicked. You have to change the document location or something like that to actually navigate between pages.

这篇关于如何触发HTML链接(锚元素)的默认动作/事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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