如果custom_field为空,如何隐藏父div

时间:2013-02-12 22:18:48

标签: javascript jquery html hide

我不熟悉js或jquery,但我需要它来完成我的“工作”。 如果(例如)custom_field(CC_STAUS)的值为空,我想隐藏父div bubble

下面的代码只是其余部分:

<div class="bubble">
   <div class="arrow"></div>
     <div class="speach" style="width: 100%;">  CC_STATUS
  </div>
</div>

和css

.bubble { 
  overflow:hidden; 
  margin:5px 0 0 0;
}
.bubble .speach { 
  background-color:#333333; 
    color:#FFFFFF;
    padding:5px;
    margin:0; 
    font-size: 12px;
    font-family: Segoe UI;
  text-transform: lowercase; 
}
.bubble .arrow {
  margin:0 0 0 15px;
    width:0;
    height:0; 
  border-left: 0px solid transparent;
    border-right: 10px solid transparent;
    border-bottom: 10px solid #333333;
    border-top: 0;
}

要检查 CC_STATUS 的长度,我尝试过这段代码(我只是改编了),但显然它不起作用。无论内容如何,​​泡沫总是会被取代。

$('.bubble').each(function() {
    if($(this).attr('CC_STATUS') === '' || $(this).text() === '') {
        $(this).parents('.bubble').hide();
    }
});

请参阅jsfiddle

提前感谢任何(工作)解决方案。

3 个答案:

答案 0 :(得分:1)

首先,你忘了加载jquery。

其次,你没有得到jquery遍历 - 你正在查看泡泡的内容,而不是说话的内容。你那里也有一个不受欢迎的.parent。

以下是解决方案:

$('.bubble').each(function() {
    if($(this).attr('CC_STATUS') === '' || $(this).find('.speach').text() === '') {
        $(this).hide();
    }
});

http://jsfiddle.net/v2Les/

编辑:除了那个$(this).attr位是完全没必要的,现在我仔细看看它。

答案 1 :(得分:0)

CC_STATUS不是气泡类元素的属性,因此您无法使用attr()。作为.bubble的孩子,'speach'类是否可靠?

如果是这样,请尝试以下方法:

$('.bubble').each(function() {
    if('.speach', $(this)).text() === '') {
        $(this).hide();
    }
}

换句话说,foreach bubble,发现它的孩子被分类为'speach',如果是空的,则隐藏泡泡。

希望能让你顺利上路。

答案 2 :(得分:0)

到目前为止,我看到答案的唯一问题是空格不是空字符串。

var re = /\w/;
var s = $(".speach").text();
var result = re.test(s);
console.log(result) // false if non-whitespace characters exist