处理点击链接自己 - JQuery Mousedown [英] Handling clicks on links myself - JQuery Mousedown
问题描述
我想自己处理特定元素的点击。
所以,如果我点击它用鼠标左键打开当前选项卡中的链接,如果我点击它与中间按钮,它将在一个新的选项卡中打开它。
我想通过从链接获取href属性并使用window.open()
这是否可能,而不会遇到弹出窗口问题? / p>
所以对于初学者,我试图阻止链接打开。
HTML:
< a href =somelink.phpclass =prev_cl>< img src =someimg.png/& / a>
Javascript:
code> $(function(){
$('。prev_cl')。on('mousedown',function(e){
return false;
});
})
但是即使这不工作,它仍然打开链接。
如果我在return false之前放置一个警告,它实际上不会触发点击并显示警报框。
我也试过使用mouseup和mousedown事件,但是没有工作
我尝试的另一件事是将返回false放在元素本身,意思是:
; a href =somelink.phponclick =return falseclass =prev_cl>< img src =someimg.png/>< / a>
然后在javascript部分我添加了window.open b
但是1)用middlemousebutton点击仍然有效2)Firefox阻止窗口打开,因为它认为它是一个弹出窗口
将处理程序放在单击
事件,而不是 mousedown
。
$(function(){$('。prev_cl')。on({'click':function(e){if(e.which == 1){var button =left;} else {button = middle;} console.log(button +click); console.log(e.which); e.preventDefault();},'contextmenu':function(e){console.log(right click); console.log(e.which); e.preventDefault();}});});
< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js>< / script>< ; a href =somelink.phpclass =prev_cl>< img src =someimg.png/>< / a>
div>
I want to handle clicks on specific elements myself. So that if I click it with the left mousebutton it opens the link in the current tab and if I click it with the middlebutton, it opens it in a new tab. I want to do this by getting the href attribute from the link and use window.open()
Is this even possible without running into popupblocker issues?
So for starters I tried to prevent the opening of the link.
HTML:
<a href="somelink.php" class="prev_cl"><img src="someimg.png" /></a>
Javascript:
$(function() {
$('.prev_cl').on('mousedown', function(e){
return false;
});
})
But even this isn't working, it still opens the link. If I put an alert before "return false" it actually doesn't trigger the click and shows the alertbox. But who wants an alert box everytime they click a link?
I also tried using both mouseup and mousedown events, but that didn't work either
Another thing I tried was putting the return false to the element itself, meaning:
<a href="somelink.php" onclick="return false" class="prev_cl"><img src="someimg.png" /></a>
Then on the javascript part I added window.open() to it
But 1) clicking with middlemousebutton still works and 2) Firefox blocks the opening of the window because it thinks it is a popup
Put your handler on the click
event, not mousedown
.
$(function() {
$('.prev_cl').on({
'click': function(e){
if (e.which == 1) {
var button = "left";
} else {
button = "middle";
}
console.log(button+" click");
console.log(e.which);
e.preventDefault();
},
'contextmenu': function(e) {
console.log("right click");
console.log(e.which);
e.preventDefault();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="somelink.php" class="prev_cl"><img src="someimg.png" /></a>
这篇关于处理点击链接自己 - JQuery Mousedown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!