简体   繁体   English

如何使用jQuery查找具有特定类和CSS样式的元素?

[英]How do I find an element with a specific class and CSS style using jQuery?

Here is what the code looks like: 代码如下所示:

<div class='class1'>
  <div class='class2'>
    <div class='class3'>
      <div class='class4'>
        <div class='class5'>
          <p>Some text 1</p>
        </div>
      </div>


      <div class='class4'>
        <div class='class5'>
          <p>Some text 1</p>
        </div>
      </div>


      <div class='class4'>
        <div class='class5' style="display:block;">
          <p>Some text 1</p>
        </div>
      </div>


      <div class='class4'>
        <div class='class5'>
          <p>Some text 1</p>
        </div>
      </div>
    </div>
  </div>
</div>

I want to get the div with class5 and CSS property display set to block . 我想将class5和CSS属性display设置为blockdiv Once I have this div I want to perform further action on that div . 一旦我有这个div我想在执行进一步行动div I tried using something like 我尝试使用类似

$('.class1 .class2 .class3 .class4').find( '.class5').is(':visible')

but it doesn't work. 但这不起作用。

The problem you have is that is() returns a Boolean, reflecting whether the passed-in element (or the first of the passed-in elements) matches the supplied argument. 您遇到的问题是is()返回一个布尔值,反映传入的元素(或传入的第一个元素)是否与提供的参数匹配。

If you switch to filter() , which filters the passed-in collection according to the supplied argument; 如果切换到filter() ,它会根据提供的参数过滤传入的集合; if the element matches then that element is retained, otherwise it's discarded: 如果元素匹配,则保留该元素,否则将其丢弃:

 let classFiveElems = $('.class1 .class2 .class3 .class4 .class5').filter( ':visible'); console.log(classFiveElems); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='class1'> <div class='class2'> <div class='class3'> <div class='class4'> <div class='class5'> <p>Some text 1</p> </div> </div> <div class='class4'> <div class='class5'> <p>Some text 1</p> </div> </div> <div class='class4'> <div class='class5' style="display:block;"> <p>Some text 1</p> </div> </div> <div class='class4'> <div class='class5'> <p>Some text 1</p> </div> </div> </div> </div> </div> 

What you want, though, is not just a simple check for visibility; 但是,您想要的不仅仅是对可见性的简单检查; but a test for a specific CSS property; 而是针对特定CSS属性的测试; so I'd suggest the following, which uses filter() but using the anonymous function: 所以我建议以下内容,它使用filter()但使用匿名函数:

 let classFiveElems = $('.class1 .class2 .class3 .class4 .class5').filter(function() { return this.style.display === 'block'; }).addClass('found'); console.log(classFiveElems); 
 .found { color: #f90; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='class1'> <div class='class2'> <div class='class3'> <div class='class4'> <div class='class5'> <p>Some text 1</p> </div> </div> <div class='class4'> <div class='class5'> <p>Some text 1</p> </div> </div> <div class='class4'> <div class='class5' style="display:block;"> <p>Some text 1</p> </div> </div> <div class='class4'> <div class='class5'> <p>Some text 1</p> </div> </div> </div> </div> </div> 

References: 参考文献:

As example: 例如:

if($('.class1 .class2 .class3 .class4')
           .find( '.class5:first')
              .is(':visible')){
  console.log('yes');
}

See: https://jsbin.com/ninovic/edit?html,js,console,output 参见: https : //jsbin.com/ninovic/edit?html,js,控制台,输出

Actually every div with class5 class has a display: block property. 实际上,每个具有class5类的div都有一个display: block属性。

display: block property is a default state of every block element ( divs are block elements). display: block属性是每个块元素的默认状态( divs是块元素)。

I've set the display property of other divs to none , just to show functionality of following code. 我已将其他div的display属性设置为none ,只是为了显示以下代码的功能。

 $('div').each(function() { if ($(this).hasClass('class5') && $(this).is(":visible")) { console.log($(this).html()); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='class1'> <div class='class2'> <div class='class3'> <div class='class4'> <div class='class5' style="display:none;"> <p>Some text 1</p> </div> </div> <div class='class4'> <div class='class5' style="display:none;"> <p>Some text 2</p> </div> </div> <div class='class4'> <div class='class5' value='t' style="display:block;"> <p>Some text 3</p> </div> </div> <div class='class4'> <div class='class5' style="display:none;"> <p>Some text 4</p> </div> </div> </div> </div> </div> 

.is() returns a boolean, you can use the pseudo-selector inside find() to select the element: .is()返回一个布尔值,可以在find()使用pseudo-selector来选择元素:

$('.class1 .class2 .class3 .class4').find('.class5:visible')

Example

$('.class5').each(function(){
  if ($(this).is(":visible")) {
   //What you want to do  
   }
});

你可以直接做

alert('is visible?', $( '.class5').is(':visible'));

This will return the .class5 having display:block property. 这将返回具有display:block属性的.class5。 IN Your case it will return all the elements. 在您的情况下,它将返回所有元素。 because all div in div contains default display property block, So it will return all of them in your case. 因为div中的所有div都包含默认的显示属性块,所以它将根据您的情况返回所有它们。 and if you try then you have assure that only the elements(.class5) you want to select have display block property. 如果尝试,则可以确保只有要选择的elements(.class5)具有显示块属性。

 var selector = $('.class1 .class2 .class3 .class4').find( '.class5').filter(function() {
                  return $(this).css('display') == 'block';
               });

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

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