點(diǎn)擊空白處隱藏相應(yīng)的元素
布局
<body>
<div id="box"></div>
<span class="btn">點(diǎn)擊此處打開(kāi)彈出層</span><br>點(diǎn)擊空白處關(guān)閉彈出層
</body>
控制
$(function(){
$(".btn").click(function(event){
var e=window.event || event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;
}
$("#box").show();
});
$("#box").click(function(event){
var e=window.event || event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;
}
});
document.onclick = function(){
$("#box").hide();
};
})
或者
$(document).mouseup(function(e){
var _con = $('.box'); // 設(shè)置目標(biāo)區(qū)域
if(!_con.is(e.target) && _con.has(e.target).length === 0){ // Mark 1
$(".box").hide();
console.log(1)
}else{
console.log(2)
}
});
這些都可以理解的不在詳解
結(jié)束?。。。。。。。?/p>