简体   繁体   English

如何防止浮动菜单被单击通过下面的IE中的链接

[英]How to prevent floating menu from being clicked through to links below in IE

All the searching I've done results in questions looking to achieve the opposite. 我所做的所有搜索都导致寻求相反的问题。 In firefox my div layer containing a menu appears, floating above the other content when clicking another element on the page. 在firefox中,出现一个包含菜单的div层,当单击页面上的另一个元素时,该层浮在其他内容上方。 The content beneath such as links are not clickable. 链接之类的内容不可单击。 This is the behaviour I want but it doesn't work that way in internet explorer. 这是我想要的行为,但是在Internet Explorer中无法正常工作。 In IE gaps between the links on the menu allow links on the content beneath this div to be clicked. 在IE中,菜单上链接之间的间隙允许单击该div下方内容上的链接。 Also a google ad which appears below the floating div is clickable through the div. 浮动div下方显示的Google广告也可以通过div点击。 I want the div to be solid and prevent any content underneath from being clicked in IE, could anyone help with this please? 我希望div牢固,并防止IE中单击任何下面的内容,有人可以帮忙吗?

event propagation 事件传播

meaning for ex that a click traverses all elements under the cursor into deep (capturing) and back (bubbling) posiblly fireing reactions from many elements ... a common mistake is to think about graphical relation instead DOM parent-child relation (between elements) 例如,单击的含义是将游标下的所有元素遍历为深层(捕获)和向后(冒泡),可能激发许多元素的反应……一个常见错误是考虑图形关系而不是DOM父子关系(元素之间)

let's experiment: 让我们进行实验:

<div id="bottomDIV" 
onclick="rBot(event)" onMouseOver="this.style.background='olive'"
onMouseOut="this.style.background='#FF6'">bottomDIV
<div id="topDIV">topDIV</div></div>
</div>

so topDIV is the firstChild of BottomDIV 所以topDIV是BottomDIV的第一个孩子

add some styling: 添加一些样式:

<style>
#bottomDIV{ width:200px;height:200px; background:#FF6;}
#topDIV{
width:200px;height:200px;
position:relative;
background:rgba(0,255,255,.5);
top:80px;left:100px;}
</style>

add reactions to events: 对事件添加反应:

  <script>
//add reaction for bottomDIV
bottomDIV=document.getElementById('bottomDIV')
rBot=function(e){evt=e||event;alert(evt.type + 
            "\n"+( evt.srcElement||evt.target).id +"\n") ;//FF/IE
}


// add reactions on same event type for topDIV 
topDIV=document.getElementById('topDIV');
rTop=function(e){
        evt=e||event;
        if(evt.target)alert(evt.target.id); else alert(evt.srcElement.id)
        }

topDIV.addEventListener('click',rTop,false);
topDIV.addEventListener('mouseover',function(){this.style.background=
                                            'rgba(200,200,100,.7)'},false)
topDIV.addEventListener('mouseout',function(){this.style.background=
                                            'rgba(0,255,255,.5)'},false)
</script>

now observe the behaviour in booth IE/FF to click and moseover; 现在观察展位IE / FF中的行为,以进行单击和移交;

Even under the cursor there's no graphical overlapping of 2 divs - to execute the topDIV reactions the event objects follows the relation parentchild and when returns fires olso the unwanted parent reactions; 即使在光标下也没有2 div的图形重叠-要执行topDIV反应,事件对象将遵循亲子关系,并且当返回时也会触发不需要的父反应; So the child is the problem :) 所以孩子是问题所在:)

SOLUTIONS: 解决方案:

1 stop the bubbling (backflow) 1停止冒泡(回流)

create a crossbrowser function Stop Event Propagation 创建一个跨浏览器功能Stop Event Propagation

function SEP(evt){
                 try{evt.stopPropagation()}
                 catch(err){;evt.cancelBubble=true;}
      } //(IE/FF)

and rewrite every reactions of topDIV like for ex: 并重写topDIV的所有反应,例如:

rTop=function(e){evt=e||event;SEP(evt);var elm=this;alert(elm.id);} 

2 - brake the relation parent-child between 2 divs append topDIV directlly to the body 2-制动2格之间的亲子关系将topDIV直接附加到身体

document.body.appendChild(topDIV);

and be sure you keep the top 并确保您保持领先

topDIV.style.zIndex=1000; 

EN:) but not EN :)但不是

I think it's a good moment to clarify .addEventListener('event', reaction , true ) which means the event fires at capturing stage 我认为现在是澄清.addEventListener('event',reaction, true )的好时机,这意味着该事件在捕获阶段触发

we are back before solutions 1,2 so we have a free flow of event parent-->child-->parent 我们在解决方案1,2之前回来了,所以我们可以自由活动父级->子级->父级

what about clicking topDIV (anywhere) we fire just bottomDIV reaction ? 点击topDIV(在任何地方)我们只触发bottomDIV反应怎么样?

justBottomReation=function(e){evt=e||event;SEP(evt);elm=this;alert(elm.id)}
bottomDIV.addEventListener('click',justBottomReation, true)

this way the click is captured by the parent that will react and SEP() will block propagation to the child. 通过这种方式,点击会被家长做出反应,并且SEP()将阻止传播到孩子。

如果您使用的是jQuery,则可以使用blockUI

I finally and accidentally discovered a workaround to stop IE behaving this way. 我最终无意中发现了一种解决方法,可以阻止IE以这种方式运行。 When I added a fixed width to the div containing the menu the problem with being able to click conent underneath the div disappeared. 当我向包含菜单的div添加固定宽度时,能够单击div下面的内容的问题消失了。

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

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