2

如果PageHeaderDescription不包含文本内容,那么我想隐藏它。用jquery可以吗?

<div class="PageHeaderDescription">
  <h1>Accessories</h1>
  <img border="0" src="" style="cursor: pointer; display: none;" id="EntityPic621">
</div>

这是 div 与文本的样子:

<div class="PageHeaderDescription">
  <h1>Accessories</h1>
  <img border="0" src="" style="cursor: pointer; display: none;" id="EntityPic621">
  This is the Accessories page, you can find stuff for the products!
</div>
4

8 回答 8

2

这变得很棘手,因为我必须比我想要的更深入 DOM,但适用于您的目标:

$('div.PageHeaderDescription').each(function(i,e){
    var txt = '';
    for (var c = 0; c < e.childNodes.length; c++){
        if (e.childNodes[c].nodeType===3){
            txt += e.childNodes[c].data;
        }
    }
    if (txt.trim().length===0){
        $(e).hide();
    }
});

再次,hacky但有效。您必须记住,在 DOM 中甚至考虑了空格,因此仅通过“没有 nodeType===3 元素”来检查/过滤是不够的。相反,您连接所有文本,然后修剪它以删除空格并获取长度。


使用自定义选择器进行后续操作:

(function($){
    $.expr[':'].notext = function(obj, index, meta, stack){
        var txt = '';
        for (var c = 0; c < obj.childNodes.length; c++){
            if (obj.childNodes[c].nodeType===3){
                txt += obj.childNodes[c].data;
            }
        }
        return (txt.trim().length===0);
    };
})(jQuery);

$('div.PageHeaderDescription:notext').hide();

当然还有工作演示

于 2012-07-19T13:13:55.287 回答
1
$("PageHeaderDescription").each(function(i, val) {
    if ($(this).text().trim() === "") {
        $(this).hide();
    }
});
于 2012-07-19T12:52:10.453 回答
1
​jQuery(document).ready(function(){

   // alert(jQuery('.PageHeaderDescription').text());

    var headerTxt = jQuery('.PageHeaderDescription h1').text();
    //警报(headerTxt);    

        if(jQuery.trim(jQuery('.PageHeaderDescription').text())==headerTxt){
        jQuery('.PageHeaderDescription').hide();
    }


});​
于 2012-07-19T12:53:59.500 回答
1

试试这个...

if( $('div.PageHeaderDescription').is(':empty') ) {
    // Your code
}
于 2012-07-19T12:55:08.563 回答
1

您将需要另一个函数来仅抓取 div 内不在某些标签内的文本。当你直接使用 .text() 时,PageHeaderDescription你也会得到 h1 的内部文本。有关您的更多信息,.justText()请参见此处!

答案是:

jQuery.fn.justText = function() {
  return $(this).clone().children().remove().end().text();
};

$('document').ready(function(){
    if ($('.PageHeaderDescription').justText().trim().length == 0) {
      $('.PageHeaderDescription').remove(); // If you want to hide, use .hide()
    }
  }
);

PS.:帮助您在 .hide() 和 .remove() 之间进行选择

-- .hide() 的参考

-- .remove() 的参考

于 2012-07-19T13:40:09.633 回答
0
 $("#empty").click(function() {
      alert($('#myDiv').is(':empty'));
    });

或者

$("div:empty").hide();

或者'

您可以使用 :is 进行检查

if( $('#leftmenu').is(':empty') ) {
}
于 2012-07-19T12:54:02.340 回答
0

首先分配一个 id= 给你 Div。

<div class="PageHeaderDescription" id="PageHeaderDescription">

然后

$("#PageHeaderDescription").each(function(i, val) {
    if ($(this).text().trim() === "") {
        $(this).hide();
    }
});
于 2012-07-19T12:54:59.360 回答
0

这对我有用:

var clone = ​$(".PageHeaderDescription").clone(true);​​​​​​​​​
clone.find('*').remove();
if (clone.text().trim() == '') $(".PageHeaderDescription").hide();
clone.remove();
于 2012-07-19T13:21:12.090 回答