javascript - 隐藏 mouseup 上的元素,除非它有内容

标签 javascript jquery jquery-events

我想在单击文档正文后隐藏一些元素。这段代码来自 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/

相关文章:

javascript - jQuery 缩小背景图像但保持高度

javascript - 如何在启动 ajax 查询的 jQuery 中触发方法 change()

javascript - 当我将鼠标快速移出绘图时,jQuery flot 工具提示不会隐藏

javascript - 在 Jssor Slider 中更改幻灯片集

javascript - 使用 context api 将数据从父级传递给子级和兄弟级子级

javascript - 使用 cheerio 执行抓取的 JavaScript

javascript - JavaScript 自定义事件或普通函数调用哪个更好?

javascript - 在网格布局中显示不同高度(分页)的加载元素

jQuery : Set Value for a Range in a Dictionary

javascript - 使用 CSS 在 HTML 中创建网格的最有效方法?