简体   繁体   English

Z-index不起作用

[英]Z-index not working

Hi I have a close button on the container and i am trying to create two different click events. 嗨我在容器上有一个关闭按钮,我正在尝试创建两个不同的点击事件。 When a user click on the close button then it should call the close button click event and when click on the container it should call the container click event. 当用户单击关闭按钮时,它应该调用关闭按钮单击事件,当单击容器时,它应该调用容器单击事件。

The container click event is working fine but when clicking on the close button then it is calling both close click event and the contianer click event. 容器点击事件工作正常,但是当点击关闭按钮时,它正在调用关闭点击事件和contianer点击事件。 I have set the z-index:9999 to the close div but still this is not working. 我已将z-index:9999设置为close div但仍然无效。

This is my Jsfiddle code . 这是我的Jsfiddle代码

$( "#close-button-landscape" ).click(function() {
  alert( "close button click" );
});
$( "#container" ).click(function() {
  alert( "container click" );
});

Thanks for the answers. 谢谢你的回答。 In the Jquery i can use event.stopPropagation(); 在Jquery中我可以使用event.stopPropagation(); but what if click events are not using Jquery and pure javascript . 但是,如果点击事件没有使用Jquery和纯javascript,该怎么办? See this updated Jsfiddle , then how to stop the bubble event? 看到这个更新的Jsfiddle ,然后如何停止泡沫事件?

What you should do is to do something called stop propagation 你应该做的是做一些叫停止传播的事情

 $( "#close-button-landscape" ).click(function(evt) {
  evt.stopPropagation();
  alert( "close button click" );
});

this prevents the click event from bubbling up to the container one as well. 这样可以防止click事件冒泡到容器中。

Note: z-index won't affect anything in this case. 注意:在这种情况下,z-index不会影响任何内容。 z-index only paints elements out of order ... and the area which #container covers is larger than the one that the close button affects z-index仅按顺序绘制元素... #container覆盖的区域大于关闭按钮影响的区域

The button is still in the container so you have to stop the click getting through: 该按钮仍在容器中,因此您必须停止点击通过:

$('#close-button-landscape').click(function(event)
{
 event=event||window.event;
 if(event.stopPropagation)
 {
  event.stopPropagation();
 }
 else
 {
  event.cancelBubble=true;
 }
}

Just use stopPropagation 只需使用stopPropagation

$( "#close-button-landscape" ).click(function(e) {
  e.stopPropagation();
  alert( "close button click" );
});
$( "#container" ).click(function() {
  alert( "container click" );
});

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

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