简体   繁体   English

使用jQuery,如何只找到可见元素并单独留下隐藏元素?

[英]Using jQuery, how do you find only visible elements and leave hidden elements alone?

So I start with items 1-4: 所以我从第1-4项开始:

<div class="someDiv bold italic" style="display: none;">Lorem</div>
<div class="someDiv regular italic" style="display: block;">Lorem</div>
<div class="someDiv bold" style="display: none;">Ipsum</div>
<div class="someDiv regular" style="display: block;">Ipsum</div>

Then I have some input checkboxes: 然后我有一些输入复选框:

<input class="regular" type="checkbox" />
<input class="bold" type="checkbox" />
<input class="italic" type="checkbox" />

So basically I have jQuery showing and hiding divs. 所以基本上我有jQuery显示和隐藏div。 Now I have another function that must iterate through these divs (one for each checkbox), and show/hide based on another criteria. 现在我有另一个必须遍历这些div的函数(每个复选框一个),并根据另一个标准显示/隐藏。 But I don't want the already hidden divs to be shown again. 但我不希望再次显示已经隐藏的div。

$(".someDiv").each(function(){
  if($(this).hasClass("regular")){
    $(this).show();
  } else {
    $(this).hide();
  };

In this example, the only remaining div should be the last div. 在这个例子中,唯一剩下的div应该是最后一个div。 Unfortunately, this code will make the second and fourth divs shown. 不幸的是,这段代码将显示第二和第四个div。

This code is very basic example of all the filters I'm going to be applying, adding etc. 这段代码是我将要应用的所有过滤器的基本示例,添加等。

You can use the :visible selector to find only visible. 您可以使用:visible选择器查找仅可见。

$(".someDiv:visible").each(....);

You can use the .not() selector to find only hidden. 您可以使用.not()选择器仅查找隐藏的。

$(".someDiv").not(":visible").each(....);

I think you can perform the same operation in your code with this one line. 我认为您可以使用这一行在代码中执行相同的操作。

$(".someDiv").hide().find(".regular").show();

Find all .someDiv and hide them, then find those with a .regular class and show them. 找到所有.someDiv并隐藏它们,然后找到那些带有.regular类并显示它们。

You could use :visible selector to select the .someDiv that are visible. 您可以使用:visible selector选择:visible.someDiv

$(".someDiv:visible").each(function(){
 if($(this).hasClass("regular")){
    $(this).show();
  } else {
    $(this).hide();
  }
});

Here is another funny way utilizing the chaining :) and making it single line. 这是利用链接的另一个有趣的方式:)并使其成为单行。

$('.someDiv:visible').not($('.someDiv.regular:visible')).hide();

You could do this two ways: You could add another class for the display: none elements and make them invisible via css, or you could find out the css property via jquery 您可以通过以下两种方式执行此操作:您可以为display: none添加另一个类display: none元素并通过css使它们不可见,或者您可以通过jquery找到css属性

via css class 通过css类

html HTML

<div class="someDiv bold italic hidden" >Lorem</div>
<div class="someDiv regular italic" >Lorem</div>
<div class="someDiv bold hidden" >Ipsum</div>
<div class="someDiv regular" >Ipsum</div>

css CSS

.someDiv{
    display: block;
}

.hidden{
    display: none;
}

js JS

$(".someDiv").each(function(){
  if($(this).hasClass("hidden")){
    $(this).show();
  } else {
    $(this).hide();
  };

via jquery 通过jquery

$(".someDiv:visible").each(function(){
 if($(this).hasClass("regular")){
    $(this).show();
  } else {
    $(this).hide();
  }
});

你可以使用:not()选择器并在进入.each()之前过滤结果:

$(".someDiv:not(:hidden)").each(function(){

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

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