如何使用 jQuery 以编程方式触发对链接的点击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2847185/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:24:06  来源:igfitidea点击:

How to programmatically trigger the click on a link using jQuery?

jquery

提问by Mohit Jain

How to programmatically trigger the click on a link using jQuery?

如何使用 jQuery 以编程方式触发对链接的点击?

采纳答案by Thomas

$('#your_link_id').click()

See the excellent jquery docsfor more information

有关更多信息,请参阅优秀的 jquery文档

回答by Jag

If you have an anchor link:

如果您有锚链接:

<a id="my_link_id" href="something">My Link</a>

it will fail as other answers have mentioned. Calling .eq and .trigger('click') doesn't work for me, but this does:

正如其他答案所提到的那样,它会失败。调用 .eq 和 .trigger('click') 对我不起作用,但这样做:

$('#your_link_id').get(0).click();

In my specific case, I've assigned a blob url programmatically to the anchor's href.

在我的特定情况下,我以编程方式为锚点的 href 分配了一个 blob url。

回答by nc3b

You can use trigger:

您可以使用触发器

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

回答by sandeep kale

$('#your_link_id')[0].trigger('click');

is needed as jQuery returns an array and we cannot trigger click on multiple eventlinks. You have to target only a single element

需要,因为 jQuery 返回一个数组,我们不能触发点击多个事件链接。你只需要定位一个元素