jQuery:如何触发锚链接的点击事件

发布于 2024-11-09 03:10:47 字数 157 浏览 0 评论 0原文

我有一个锚链接,例如

<a id="myanchor" href="http://google.com" target="_blank">Google</a>

如何以编程方式在新选项卡中打开 href 目标?

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?

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

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

发布评论

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

评论(7

我很坚强 2024-11-16 03:10:47

请尝试以下操作:

$("#myanchor")[0].click()

就这么简单。

Try the following:

$("#myanchor")[0].click()

As simple as that.

背叛残局 2024-11-16 03:10:47

调用 click 事件(不执行重定向)和导航到 href 位置是有区别的。

导航:

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

在新选项卡或窗口中打开:

 window.open($('#myanchor').attr('href'));

调用单击事件(调用 javascript):

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

There's a difference in invoking the click event (does not do the redirect), and navigating to the href location.

Navigate:

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

Open in new tab or window:

 window.open($('#myanchor').attr('href'));

invoke click event (call the javascript):

 $('#myanchor').click();
━╋う一瞬間旳綻放 2024-11-16 03:10:47

尽管这篇文章是 caput 的,但我认为它很好地演示了 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'));
  }
};

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 need to 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.open pretty easily if you need to:

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'));
  }
};
薆情海 2024-11-16 03:10:47
 window.open($('#myanchor').attr('href'));

               $('#myanchor')[0].click();
 window.open($('#myanchor').attr('href'));

               $('#myanchor')[0].click();
蓝梦月影 2024-11-16 03:10:47

它对我有用:

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

It worked for me:

     window.location = $('#myanchor').attr('href');
陈甜 2024-11-16 03:10:47
$(":button").click(function () {                
                $("#anchor_google")[0].click();
            });
  1. 首先,如果未给出 id,则按类型(使用“:”)查找按钮。
  2. 其次,通过 id 或在 div 等其他标签中查找锚标签,并且 $("#anchor_google")[0] 返回 DOM 对象。
$(":button").click(function () {                
                $("#anchor_google")[0].click();
            });
  1. First, find the button by type(using ":") if id is not given.
  2. Second,find the anchor tag by id or in some other tag like div and $("#anchor_google")[0] returns the DOM object.
如何视而不见 2024-11-16 03:10:47

您无法以编程方式在新选项卡中打开,这是浏览器功能。您可以在外部窗口中打开链接。看看这里

You cannot open in a new tab programmatically, it's a browser functionality. You can open a link in an external window . Have a look here

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