gpt4 book ai didi

jquery - 在 div 外部单击时隐藏它

转载 作者:技术小花猫 更新时间:2023-10-29 12:09:35 25 4
gpt4 key购买 nike

这个问题已被问过多次,但似乎没有一个答案对我有用。

div的css如下:

#info{
display: none;
position: fixed;
z-index: 500;
height: 50%;
width: 60%;
overflow: auto;
background: rgba(187, 187, 187, .8);
}

我尝试使用以下代码:

$("#info").click(function(e){
e.stopPropagation();
});

$(document).click(function(){
$("#info").hide();
});

还有这段代码:

$(document).mouseup(function (e){
var container = $("#info");

if (container.has(e.target).length === 0) {
container.hide();
}
});

然而每当我点击 div 它也消失了,不知道为什么但它确实消失了。
还有其他可能有用的东西吗?

最佳答案

因为你的目标有id=info,所以你可以尝试:

$(document).click(function(e) {

// check that your clicked
// element has no id=info

if( e.target.id != 'info') {
$("#info").hide();
}
});

你也可以试试:

$(document).click(function() {

if( this.id != 'info') {
$("#info").hide();
}

});

根据评论

$(document).click(function(e) {

// check that your clicked
// element has no id=info
// and is not child of info
if (e.target.id != 'info' && !$('#info').find(e.target).length) {
$("#info").hide();
}
});

关于jquery - 在 div 外部单击时隐藏它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11545518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com