Chrome:“在新选项卡中打开链接”不触发点击事件?
我正在开发一个 Chrome 扩展程序,当在网页中单击 标记时,该扩展程序会执行某些操作。
下面是一些示例代码:
HTML:
<table>
<tr>
<td id="mytest"><a href="http://blablabla.com">Foo Bar</a></td>
</tr>
</table>
Javascript:
var myTd = document.getElementById("mytest");
myTd.addEventListener("click", function() {
localStorage["foobar"] = 1;
});
当我单击链接时,会设置 localStorage
键,如果我用鼠标中键单击它,它也会设置该键(并在新标签)。
问题是当我使用右键单击和“在新选项卡中打开链接”时。在这种情况下,单击事件似乎不会被触发,因此不会设置 localStorage
键。
我错过了什么吗?有什么办法可以让右键-> “在新选项卡中打开链接”触发点击事件?
请注意,我不想将侦听器添加到 节点,因为我正在处理的实际 HTML 存在一些复杂性。
I'm developing a Chrome extension that does something when a <td>
tag is clicked in a web page.
Here's some sample code:
HTML:
<table>
<tr>
<td id="mytest"><a href="http://blablabla.com">Foo Bar</a></td>
</tr>
</table>
Javascript:
var myTd = document.getElementById("mytest");
myTd.addEventListener("click", function() {
localStorage["foobar"] = 1;
});
When I click the link, the localStorage
key is set, if I click it with the mouse middle button, it also sets the key (and opens the link in a new tab).
The problem is when I use right-click and "open link in a new tab". In this case the click event doesn't seem to be fired and therefore the localStorage
key will not be set.
Am I missing something? Is there any way to make the right-click -> "open link in new tab" fire the click event?
Please note that I don't want to add the listener to the <a>
node, because of some complications in the real HTML I'm working on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好问题...
浏览器上没有右键单击事件,chrome 发送事件 mousedown、mouseup 和 contextmenu,
我发现以下网页非常有用,尽管我没有检查右键部分,事件链的一般描述是相当忠实。
快速参考:http://unixpapa.com/js/mouse.html
nice question...
There is not a rightclick event on browser, chrome send the events mousedown, mouseup and contextmenu,
I found the following webpage quite useful, though I've not checked the rightbutton part, the general description of chain of events is quite faithful.
For a quick reference: http://unixpapa.com/js/mouse.html
使用
mousedown
事件代替click
:这样即使用户选择“在新选项卡中打开链接”,它仍然有效。
Use
mousedown
event in place ofclick
:In this way even if the user chooses to "Open link in a new tab", it still works.