简体   繁体   English

无法触发“ a”元素上的点击事件

[英]Can't trigger a click event on “a” element

I'm trying to trigger a click event on an <a> element, but nothing happens. 我正在尝试触发<a>元素上的click事件,但是什么也没有发生。 I want that the link included on href open a new tab. 我希望href上包含的链接打开一个新标签。

I've used: 我用过:

$("#tables a").eq(0).click();

Anybody knows why is not working? 有人知道为什么不起作用吗?

click() will trigger the event, but not actually click the link. click()将触发事件,但不会实际单击该链接。 The closest you can get is window.open : 您可以获得的最接近的是window.open

window.open($('#tables a').get(0).href);

Or, to open it in the same page: 或者,在同一页面中打开它:

window.location.href = $('#tables a').get(0).href;

Nevermind, turns out .click has always existed (keanu-whoa). 没关系,事实证明.click一直存在(keanu-whoa)。 For posterity's sake: 为了后代的缘故:

.click is not a method on DOMElement (which is what $(...)[0] will be). .click不是DOMElement上的方法($(...)[0]将是该方法)。

 $("#tables a").first().click(); 

There's also .eq() if you need something other than the first link. 如果您需要除第一个链接以外的其他内容,则还有.eq()。

API reference: API参考:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLAnchorElement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM