简体   繁体   English

如果内容为空,则隐藏div

[英]Hide a div if content is empty

Philosophy bubble is like a quote/speech bubble div styled which has a sharepoint control inside, the richHtmlField which lets users to type in content while editing page, but if the user chooses to leave it empty, there will be no content in the div so only the bubble will show up in the page which will look funny so i wanna hide the whole div when there is no user entry or basically the div is empty?? 哲学泡沫就像一个引用/语音泡沫div样式,内部有一个sharepoint控件,richHtmlField允许用户在编辑页面时输入内容,但如果用户选择将其留空,则div中没有​​内容只有气泡会出现在页面中看起来很有趣所以我想在没有用户输入或基本上div是空的时候隐藏整个div? How do you do this in jquery? 你是如何在jquery中做到这一点的?

<div class="philosophy-bubble">
<PublishingWebControls:RichHtmlField FieldName="carephilosophy" runat="server"></PublishingWebControls:RichHtmlField>
</div>  

Use jQuery's :empty selector: 使用jQuery的:empty选择器:

$('.philosophy-bubble:empty').hide();

Here's a working fiddle . 这是一个工作小提琴

Alternative 替代

You could also use the filter() function to find all empty div's and hide: 您还可以使用filter()函数查找所有空div并隐藏:

//All divs
$('div').filter(function() {
        return $.trim($(this).text()) === ''
}).hide()

//div's with a certain class
$('.philosophy-bubble').filter(function() {
        return $.trim($(this).text()) === ''
}).hide()

//div with a specific ID
$('#YourDivID').filter(function() {
        return $.trim($(this).text()) === ''
}).hide()

..etc. ..等等。

Note: the :empty selector will be more performant. 注意:: :empty选择器性能更高。 See jsPerf . 见jsPerf

var myDiv =  $('#myDiv');

if (myDiv.text() == '')
{
    myDiv.hide();
}

Something like: 就像是:

if ($('.philosophy-bubble').is(":empty")){
    $('.philosophy-bubble').hide();
}

here is a fiddle: http://jsfiddle.net/IrvinDominin/XYr9T/1/ 这是一个小提琴: http//jsfiddle.net/IrvinDominin/XYr9T/1/

EDIT 编辑

better by using: 更好地使用:

$('.philosophy-bubble:empty').hide()

http://jsfiddle.net/IrvinDominin/XYr9T/3/ http://jsfiddle.net/IrvinDominin/XYr9T/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM