简体   繁体   English

如何使底层元素不可点击/无效?

[英]How to make an underlaying element unclickable/deactive?

I have two elements on top of each other. 我彼此之间有两个要素。 When I click a button on the first div, the second div opens on top of the first div, and what I want to do is to make the underlaying div non-interactive (That I can't click on anything on the underlaying-div as long as the overlaying-div is open). 当我单击第一个div上的按钮时,第二个div在第一个div的顶部打开,我要做的是使基础div不互动(即我无法在基础div上单击任何内容(只要overlaying-div打开)。

Javascript code: JavaScript代码:

  $('#button').live('click', function()
  {    
      $('#underlaying-div).fadeTo("fast", 0.7);
      $('#overlaying-div).css('display', 'block');

      //Do something here to make the underlaying div unclickable
  });

  $("#overlaying-div").live("click", function() {
     $(this).hide();

     $('#underlaying-div).fadeTo("slow", 1.0);

     //Do something here to make the underlaying div clickable again
  });

CSS-code: CSS代码:

 #overlay-div
 {
    position:absolute;
    left:0;
    top:0;
    display:none;
    z-index: 20000;    
 }

I know I can use event.preventDefault() to make sure nothing happens if you click on an element in the underlaying-div, but I'd rather want that nothing happens at all when you for instance hover over an button (with preventDefault(), hover and other stuff still happens). 我知道我可以使用event.preventDefault()来确保如果单击underlaying-div中的某个元素,则不会发生任何事情,但是我希望当您将鼠标悬停在按钮上时,什么也不会发生(使用preventDefault( ),悬停和其他内容仍然发生)。

Any other ways in CSS or javascript/JQuery that can fix this problem?? CSS或javascript / JQuery中的其他任何方法都可以解决此问题?

Not sure of your final product, but if the underlaying div get overlapped by the overlaying in a way that the underlaying div is not visible anymore you could just display:block; 不确定最终产品,但是如果底层div被覆盖重叠而导致底层div不再可见,则可以只display:block; the underlaying div. 底层div

This is a very old question, but if someone happens upon it, you might find it useful to toggle the pointer-events CSS property of the object you want to disable. 这是一个非常老的问题,但是如果有人碰到它,您可能会发现切换要禁用的对象的指针事件CSS属性很有用。 You won't need to manually remove click bindings or add any other wrappers. 您无需手动删除单击绑定或添加任何其他包装。 If an object has pointer-events set to 'none', no events will fire when it is clicked. 如果对象的指针事件设置为“无”,则单击该对象时不会触发任何事件。

In jQuery: 在jQuery中:

$('#underlaying-div).css('pointerEvents', 'none'); // will disable
$('#underlaying-div).css('pointerEvents', 'auto'); // will reenable

You could use unbind to remove the click event handler like this: 您可以使用unbind删除click事件处理程序,如下所示:

$(this).unbind('click'):

My concern is if this works with a live bind but you should at least try it :) 我担心的是,这是否可以用于实时绑定,但您至少应该尝试一下:)

Why don't you use jQuery .fadeIn() and .fadeOut() functions? 为什么不使用jQuery .fadeIn().fadeOut()函数? You have two divs with id="div1" and id="div2" and you have a button in div1 with id="button1" and a button in div2 with id="button2" . 您有两个具有id="div1"id="div2" div,并且在div1具有id="button1"的按钮和在div2具有id="button2"的按钮。 CSS code: CSS代码:

#div1 {
   //some CSS code without z-index
}

#div2 {
   //some CSS code without z-index
   visibility:hidden;
}

jQuery code: jQuery代码:

$('#button1').click(function(){$('#div1').fadeOut('slow', function(){$('#div2').fadeIn()})})
$('#button2').click(function(){$('#div2').fadeOut('slow', function(){$('#div1').fadeIn()})})

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

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