如何在隐藏父元素后显示子元素?

huangapple go评论92阅读模式
英文:

How do I unhide child elements after the parent has been hidden?

问题

I'm not sure what I need here but I am working on filtering results. Within the content div are the rows I am filtering. This is just one of the rows. When I hide .m-music, JQuery hides all of the child elements as well. When I want to show everything, I thought I could just do:

$("div.content div.m-music").show();

and have it show all its children, but that's not what's happening. It will only unhide the .m-music div. How can I show everything? Do I need a loop?

<div class="content">
  <div class="m-music">  <----------------This and below should be unhidden
    <div class="tas"></div>
    <div class="data-row">
      <div class="info">
        <table>
          <tbody>
            <tr>
              <td>this should be unhidden</td>
              <td>this should be unhidden</td>
            </tr>
          </tbody>
        </table>
        <div>this should be unhidden</div>
      </div>
      <div class="user-post">
        this should be unhidden
      </div>
    </div>
  </div>
</div>

So, I can see from the snippet that was posted below that only the parent is being hidden. I think I know why now after looking at the JQuery.

$("div.content div").hide(); //hide all

Is it because this div.content div is recursively setting all the divs to hidden? If so, how do I only hide the parent?

英文:

I'm not sure what I need here but I am working on filtering results. Within the content div are the rows I am filtering. This is just one of the rows. When I hide .m-music, JQuery hides all of the child elements as well. When I want to show everything, I thought I could just do:

$(&quot;div.content div.m-music&quot;.show();

and have it show all its children, but that's not what's happening. It will only unhide the .m-music div. How can I show everything? Do I need a loop?

  &lt;div class=&quot;content&quot;&gt;
    &lt;div class=&quot;m-music&quot;&gt;  &lt;----------------This and below should be unhidden
      &lt;div class=&quot;tas&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;data-row&quot;&gt;
        &lt;div class=&quot;info&quot;&gt;
          &lt;table&gt;
            &lt;tbody&gt;
              &lt;tr&gt;
                &lt;td&gt;this should be unhidden&lt;/td&gt;
                &lt;td&gt;this should be unhidden&lt;/td&gt;
              &lt;/tr&gt;
            &lt;/tbody&gt;
          &lt;/table&gt;
          &lt;div&gt;this should be unhidden&lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;user-post&quot;&gt;
          this should be unhidden
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;

So, I can see from the snippet that was posted below that only the parent is being hidden. I think I know why now after looking at the JQuery.

$(&quot;div.content div&quot;).hide(); //hide all

Is it because this div.content div is recursively setting all the divs to hidden? If so, how do I only hide the parent?

答案1

得分: 0

当你隐藏一个类时,它会被赋予style="display:none;"属性,所有子元素都会被隐藏,但它们都不会被赋予这个属性,所以当你再次显示该类时,子元素将再次变为可见。

所以,如果在你的代码中没有发生这种情况,我建议可能在其他地方存在问题。

我创建了一个片段来演示如何在循环中隐藏和显示元素。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
setInterval(function() {
  $("div.content div.m-music").hide();
}, 2000)
setTimeout(function() {
  setInterval(function() {
    $("div.content div.m-music").show();
  }, 2000)
}, 1000)

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
  <div class="m-music">
    <div class="tas"></div>
    <div class="data-row">
      <div class="info">
        <table>
          <tbody>
            <tr>
              <td>this should be unhidden</td>
              <td>this should be unhidden</td>
            </tr>
          </tbody>
        </table>
        <div>this should be unhidden</div>
      </div>
      <div class="user-post">
        this should be unhidden
      </div>
    </div>
  </div>
</div>

<!-- end snippet -->

希望这对你有帮助。

英文:

When you hide a class, it is given a style=&quot;display:none;&quot; attribute and any children elements are hidden, but none of them are given this attribute so when you show that class again, the children elements will become visible again.

So if that's not happening in your code I suggest that there might be another problem somewhere.

I made a snippet to demonstrate this hiding and showing on a loop.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

setInterval(function() {
  $(&quot;div.content div.m-music&quot;).hide();
}, 2000)
setTimeout(function() {
  setInterval(function() {
    $(&quot;div.content div.m-music&quot;).show();
  }, 2000)
}, 1000)

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;content&quot;&gt;
  &lt;div class=&quot;m-music&quot;&gt;
    &lt;div class=&quot;tas&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;data-row&quot;&gt;
      &lt;div class=&quot;info&quot;&gt;
        &lt;table&gt;
          &lt;tbody&gt;
            &lt;tr&gt;
              &lt;td&gt;this should be unhidden&lt;/td&gt;
              &lt;td&gt;this should be unhidden&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
        &lt;div&gt;this should be unhidden&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;user-post&quot;&gt;
        this should be unhidden
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 06:18:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定