页面中间点击监听器 [英] Page Middle Click Listener
问题描述
当我使用中点击时,我需要弹出警报,如果我在链接上单击它,但是在页面的任何其他元素(或者只是在空白空间上)单击它,它都不会弹出。
pre $ var $ test $ {$ b $ pageMiddleClickListener:function(e){
if(e.which === 2){
// if(!gContextMenu.onLink){
alert('ok');
//
window.addEventListener(click,test.pageMiddleClickListener,false);
当我使用中间点击链接时,警报显示,但我需要防止此行为链接
我需要类似!gContextMenu.onLink但不是用于上下文菜单(无)
您可以使用多种方式测试点击链接的目标
。一种方法是检查是否 Element.tagName
是 A
,另一个是测试 href
属性。事实证明,还有必要测试目标的 parentNode
是否是链接。
<$ p $($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ e.target)){
e.view.alert('ok');
$ b isLinkOrAParentIsLink:function(el){
if(el.tagName ===A){
返回true;
} // else
let parent = el.parentNode;
while(parent!== null&& typeof parent.tagName!==undefined){
if(parent.tagName ===A){
return true ;
} // else
parent = parent.parentNode;
}
返回false;
window.addEventListener(click,test.pageMiddleClickListener,false);
或
isLinkOrAParentIsLink:function(el){
if(el.hasAttribute(href)){
return true;
} // else
let parent = el.parentNode;
while(parent!== null&& typeof parent.tagName!==undefined){
if(parent.hasAttribute(href)){
return true;
} // else
parent = parent.parentNode;
}
返回false;
}
注意:我更改了 e.which
to e.button
,因为这是点击事件和 MouseEvent.which
是非标准的。注意这也需要测试 e.button === 1
而不是2。
I need to pop-up alert when I use Middle Click and it must don't pop-up if I click it on a link but on any other element of page (or just on empty space).
var test = {
pageMiddleClickListener : function(e) {
if(e.which === 2) {
//if (!gContextMenu.onLink) {
alert('ok');
//}
}
}
window.addEventListener("click",test.pageMiddleClickListener,false);
Alert show-ups when I use Middle Click on a link, but I need to prevent this behavior to links
I need something like "!gContextMenu.onLink" but not for context menu (without)
There are multiple ways that you can test for the target
of the click being a link. One way would be to check to see if the Element.tagName
is A
, another would be to test for the href
property. As it turns out is is also necessary to test to see if any of the target's parentNode
s are links.
var test = {
pageMiddleClickListener : function(e) {
if(e.button === 1) {
if (!test.isLinkOrAParentIsLink(e.target)) {
e.view.alert('ok');
}
}
},
isLinkOrAParentIsLink : function(el) {
if (el.tagName === "A") {
return true;
} //else
let parent= el.parentNode;
while (parent !== null && typeof parent.tagName !== "undefined") {
if (parent.tagName === "A") {
return true;
} //else
parent= parent.parentNode;
}
return false;
}
}
window.addEventListener("click",test.pageMiddleClickListener,false);
or
isLinkOrAParentIsLink : function(el) {
if (el.hasAttribute("href")) {
return true;
} //else
let parent= el.parentNode;
while (parent !== null && typeof parent.tagName !== "undefined") {
if (parent.hasAttribute("href")) {
return true;
} //else
parent= parent.parentNode;
}
return false;
}
Note: I changed e.which
to e.button
as that is what is in the specification for click events and MouseEvent.which
is non-standard. Note this also required testing for e.button === 1
instead of 2.
这篇关于页面中间点击监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!