简体   繁体   English

如果标题标签为空,则隐藏div吗?

[英]Hide a div if the header tag within it is empty?

I have a carousel where I am trying to hide the description for certain slides if no content is being output dynamically. 我有一个轮播,如果没有动态输出内容,我会在其中隐藏某些幻灯片的描述。

My structure with content being output: 我的内容输出结构:

<div class="caption">
   <h3><a>title</a></h3>
   <p><a>description</a></p>
</div>

My structure without: 我的结构没有:

<div class="caption">
   <h3><a></a></h3>
   <p><a></a></p>
</div>

Javascript: Javascript:

if($.trim($('.caption h3 a').text()) != "") {
$(".carousel-caption.h").addClass("visible");
}

I got the javascript from looking through similar questions on here. 通过查看类似的问题,我得到了javascript。 I have tried various ways of doing this but to no avail. 我尝试了各种方法来执行此操作,但无济于事。 I am not quite sure what I am missing? 我不太确定自己缺少什么?

You can do this to hide all .caption having an empty header : 您可以执行以下操作以隐藏所有具有空标题的.caption

$('.caption').show().filter(function(){
   return $.trim($('h3', this).text())===''
}).hide();

This might be nearer from your initial solution, changing only the class when the head has a content : 这可能与您最初的解决方案更接近,仅当head包含内容时才更改类:

$('.caption').each(function(){
    if ($.trim($('h3', this).text())!=='') $(this).addClass('visible');
});

I am not a jQuery guy. 我不是jQuery的人。 But in YUI it can be simply like: 但是在YUI中,它可能就像:

if (Y.one(".caption h3 a").get("innerHTML") == "") {
    alert("its empty");
}

try using .html instead of .text in jquery 尝试在jQuery中使用.html而不是.text

this works, 这有效

 if($('.caption h3 a').text().trim() != "") {
       $(".carousel-caption.h").addClass("visible");
 }

FIDDLE 小提琴

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

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