如何“触发”使用 Javascript 在特定锚元素上单击事件?最好使用 jQuery

发布于 2024-09-01 12:18:33 字数 200 浏览 1 评论 0原文

我想要做的是在锚元素上发出一个单击事件(最好使用 jQuery),使浏览器跟随该链接。我知道我的锚选择器是正确的,因为它们从 .length 返回 1。

这是我尝试过的:

$('#awesomeLink').click();

但它不会点击链接。

我怎么做错了?

谢谢!!

What I want to do is issue a click event, preferably with jQuery, on an anchor element, causing the browser to follow that link. I know that my anchor selectors are correct, as they are returning 1 from .length.

Here's what I've tried:

$('#awesomeLink').click();

But it won't follow the link.

How am I doing it wrong?

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一桥轻雨一伞开 2024-09-08 12:18:33

为什么不直接解析 url 并将用户发送到该链接位置呢?

var url = $('#awesomeLink').attr('href');
window.location.href = url;

Why not just parse the url and send the user to that link location?

var url = $('#awesomeLink').attr('href');
window.location.href = url;
鹿! 2024-09-08 12:18:33

尝试 $('#awesomeLink').trigger("click");

Try $('#awesomeLink').trigger("click");

情绪少女 2024-09-08 12:18:33

不要尝试直接单击链接,只需告诉浏览器加载不同的页面:

window.location = "my_link's_URL";

这个问题类似于 如何自动点击a-link-with-javascript

Instead of trying to click the link directly, just tell the browser to load a different page:

window.location = "my_link's_URL";

This question is similar to how-do-i-automatically-click-a-link-with-javascript.

戴着白色围巾的女孩 2024-09-08 12:18:33

如果您只是想更改浏览器位置,请尝试:

window.location.href = $('#awesomeLink').attr('href');

否则,如果您需要更多信息,那么 这个问题 可能会有所帮助(关于本机点击的其他问题也可能有所帮助)。

If you are just wanting to change the browser location try:

window.location.href = $('#awesomeLink').attr('href');

Otherwise if you are needing a bit more, then this question might help (as might the other questions about native clicking).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文