0

I need to use JavaScript to trigger a page to open in a new window when clicking the link. I've noticed that when you middle-click on the link in Firefox, you would open the current page in a new window instead of another page in a new window.

Note that I cannot do the following. This is because another application needs to read my code, and the link must be invoked via JavaScript.

<a href="http://www.google.ca" target="_blank">Link</a>

http://jsfiddle.net/4BuTg/

$('#link').click( function(event) {
  event.preventDefault();
  window.open( "http://www.google.ca", '_blank' );
});

<a href="" id="link" target="_blank">Middle Click This Link</a>
4

3 回答 3

2

您可以检查鼠标按下事件

http://api.jquery.com/event.which/

event.which 还标准化按钮按下(mousedown 和 mouseupevents),报告左键为 1,中键为 2,右键为 3。使用 event.which 而不是 event.button。

于 2013-06-07T22:48:04.940 回答
1

您可以尝试简单地将默认行为添加回链接。请注意,此代码未经测试,但我认为它应该解决中间按钮问题:

$(document).ready(function () {
    $('#link').attr('href','http://www.google.com');
    $('#link').attr('target','_blank');
});

但是,如果这不起作用,请进一步解释链接必须由 js 调用的意思。

于 2013-06-07T22:50:48.797 回答
0

而不是使用.click(),我应该使用.bind()

于 2013-06-10T15:35:36.913 回答