简体   繁体   English

JQuery测试HTML内容的类/有条件地设置元素的CSS的内容

[英]JQuery Test HTML Content of Class/Conditionally Set CSS of element

I have a bunch of divs with the class "eac-item". 我有一班“ EAC项目”的div。

Some of the items have content - but others are "blank" and only come up with this: 其中一些项目具有内容-但其他项目是“空白”的,只能提出以下内容:

<div class="eac-item"><br></div>

What would the syntax be in JQuery to add a CSS property of "display:hidden" to those specific items? 在JQuery中向这些特定项目添加CSS属性“ display:hidden”的语法是什么?

if($('.eac-item').html() == "<br>") { {$(this).hide } ...?  

How do you handle the syntax for this type of function? 您如何处理此类函数的语法?

One way to hide the elements (add display: none ) would be iterate through every .eac-item using each() : 隐藏元素的一种方法(添加display: none )将使用each()遍历每个.eac-item

 $('.eac-item').filter(function(index) { // Filter all elements whose only child is a <br> return $("*", this).length === 1 && $("br", this).length === 1; }).each(function() { $(this).hide(); }); 
 .eac-item { border: 2px #000 solid; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="eac-item"> <br> </div> <div class="eac-item">This is not hidden.</div> 

You might want to use remove() instead of hide() , if you're really trying to remove the element from DOM. 如果您确实要从DOM中删除元素,则可能要使用remove()而不是hide()

Following code should do the trick 以下代码应该可以解决问题

$(".eac-item").each(function(){
  if ($(this).html() == "<br>")
    $(this).hide();
});

but i would suggest to go with below approach 但我建议采用以下方法

 $(".eac-item").each(function(){
      if ($.trim($(this).text()) == "")
        $(this).hide();
    });

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

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