我想在单击文档正文后隐藏一些元素。这段代码来自 Stack Overflow 中给出的答案:
$(document).mouseup(function (e){
var container = $(".time-stamp, .full-name, .comment-box-wrapper .search-result");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // nor a descendant of the container
{
container.hide();
}
});
它工作正常,但我的页面一次显示 10 篇不同的文章,并且每篇文章都有文本框(comment-box
),它们包含在 comment-box-wrapper
中,并且附在每篇文章中,用户可以
提交他们的意见。这些评论框默认设置为隐藏,直到用户单击评论按钮。问题是如果用户有
在评论框中输入一些文本并单击其他位置,comment-box
被设置为隐藏
并且内容完全丢失。
如果comment-box
中已经有一些内容,如何取消.hide()
?
最佳答案
$(document).mouseup(function (e){
var container = $(".time-stamp, .full-name, .comment-box-wrapper.not-active .search-result");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // nor a descendant of the container
{
container.hide();
}
});
$('.comment-box').on('change', function(){
if(this.value!=""){
$(this).parent().removeClass('not-active');
}
})
关于javascript - 隐藏 mouseup 上的元素,除非它有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29729588/