[英]How to hide parent div if custom_field is empty
I am not familiar with js or jquery but I need that to finish my "work". 我对js或jquery不熟悉,但是我需要这样做来完成我的“工作”。 I want to hide parent div bubble if value of, let say, custom_field (CC_STAUS) is empty.
如果想让custom_field(CC_STAUS)的值为空,我想隐藏父div 气泡 。
Below code is only a part of the rest: 下面的代码只是其余部分的一部分:
<div class="bubble">
<div class="arrow"></div>
<div class="speach" style="width: 100%;"> CC_STATUS
</div>
</div>
and css 和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;
}
To check the lenght of the CC_STATUS I have tried this code (which I simply adapted) but obviously it does not work. 为了检查CC_STATUS的长度,我尝试了此代码(我只是对其进行了修改),但显然不起作用。 The bubble is always displaied no matter of it content.
不论泡沫内容如何,泡沫总是被取代。
$('.bubble').each(function() {
if($(this).attr('CC_STATUS') === '' || $(this).text() === '') {
$(this).parents('.bubble').hide();
}
});
Thanks in advance for any (working) solution. 在此先感谢您提供任何(有效的)解决方案。
First of all, you forgot to load jquery. 首先,您忘记加载jquery。
Secondly, you didn't get the jquery traversal quite right - you were looking at the contents of the bubble, not the contents of speach. 其次,您没有正确遍历jquery-您正在查看气泡的内容,而不是speach的内容。 You also had an unwanted .parent in there.
您还在那里有一个不需要的.parent。
Here's the solution: 解决方法如下:
$('.bubble').each(function() {
if($(this).attr('CC_STATUS') === '' || $(this).find('.speach').text() === '') {
$(this).hide();
}
});
http://jsfiddle.net/v2Les/ http://jsfiddle.net/v2Les/
Edit: Except that that $(this).attr bit is completely unnecessary, now that I take a closer look at it. 编辑:除了$(this).attr位完全没有必要,现在我仔细看一下。
CC_STATUS is not an attribute of a bubble class element, so you can't use attr()
. CC_STATUS不是冒泡类元素的属性,因此不能使用
attr()
。 Is the 'speach' class reliable as a child of .bubble? 作为“气泡”的孩子,“演讲”类是否可靠?
If so, try something like this: 如果是这样,请尝试以下操作:
$('.bubble').each(function() {
if('.speach', $(this)).text() === '') {
$(this).hide();
}
}
In other words, foreach bubble, find its child classed 'speach' and if empty, hide the bubble. 换句话说,foreach气泡找到其子级为“ speach”,如果为空,则将其隐藏。
Hopefully that gets you on your way. 希望这能帮助您前进。
The only problem I see with the answers so far is whitespace is not empty string. 到目前为止,我在答案中看到的唯一问题是空格不是空字符串。
var re = /\w/;
var s = $(".speach").text();
var result = re.test(s);
console.log(result) // false if non-whitespace characters exist
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.