2016-08-14 82 views
1

我做了这段代码,以便一旦点击“内部div”,点击“pointer-events:none;”就可以转到“outer div”。在CSS中。使用指针事件点击重叠的div并隐藏div

$("#outer").click(function(){ 
 
alert("outer clicked") 
 

 
}); 
 

 
$("#inner").click(function(e){ 
 
    alert("inner clicked") 
 
    this.style.display = 'none'; 
 
    e.stopPropagation(); 
 
});
#outer { 
 
    width:300px; 
 
    height:200px; 
 
    border:3px solid; 
 
    margin:auto; 
 
    background: green; 
 

 
} 
 
#inner { 
 
    pointer-events: none; 
 
    position: absolute; 
 
    top: 150px; 
 
    left: 120px; 
 
    width: 100px; 
 
    height: 100px; 
 
    background: yellow; 
 
    border:3px solid; 
 
    margin:auto; 
 
    
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="outer"> 
 
    <div id="inner">inner div</div> 
 
    Outer div 
 
</div>

OK,功能完善。但是如果你点击外部div内的任何地方,我需要内部div来隐藏。我尝试使用this.style.display = 'none';,但它不起作用。

+0

将$(this)用于DOM中当前选定的元素。 $(本)的CSS( “显示”, “无”);应该做的伎俩。 – NMO

回答

1

您只需要一个事件侦听器#outer,当点击#outer时隐藏内部div。

$('#outer').click(function (e) { 
 
    $('#inner').hide(); 
 
});
#outer { 
 
    width:300px; 
 
    height:200px; 
 
    border:3px solid; 
 
    margin:auto; 
 
    background: green; 
 

 
} 
 
#inner { 
 
    pointer-events: none; 
 
    position: absolute; 
 
    top: 150px; 
 
    left: 120px; 
 
    width: 100px; 
 
    height: 100px; 
 
    background: yellow; 
 
    border:3px solid; 
 
    margin:auto; 
 
    
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="outer"> 
 
    <div id="inner">inner div</div> 
 
    Outer div 
 
</div>

0

$("#outer").click(function(){ 
 
alert("outer clicked") 
 

 
}); 
 

 
$("#inner").click(function(e){ 
 
    alert("inner clicked") 
 
    this.style.display = 'none'; 
 
    e.stopPropagation(); 
 
});
#outer { 
 
width:300px; 
 
height:200px; 
 
border:3px solid; 
 
margin:auto; 
 
background: green; 
 
position:relative; 
 

 
} 
 
#inner { 
 
    position: absolute; 
 
    top: 150px; 
 
    left: 120px; 
 
    width: 100px; 
 
    height: 100px; 
 
    background: yellow; 
 
    border:3px solid; 
 
    margin:auto; 
 
    z-index:10; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="outer"> 
 
    <div id="inner">inner div</div> 
 
    Outer div 
 
</div>

在您的例子,使用pointer-events是允许的点击或点击行为,以“穿越”的元素到另一个元素。

+0

@isabela事实上,我不明白你为什么需要“指针事件:无”。这是你需要的东西吗? –

+0

指针事件它的点击是下面的div ...当发生点击''内'',他去“外”。 Understandy?对不起我的英语:( –