简体   繁体   English

仅显示和隐藏 <div> 使用jQuery .hide / .show

[英]Show and Hide only one <div> using jQuery .hide/.show

The .show and .hide jQuery code works for every <span> with the class .q_open and q_close . .show.hide jQuery代码适用于每<span>与类.q_openq_close I'm well aware that my because all my <div> tags have the class of .answer so they will show and hide both answers when I click on any <span> . 我很清楚,因为我所有的<div>标记都具有.answer类,因此当我单击任何<span>时,它们将显示和隐藏两个答案。 Is there a way to reference only one <div class=".answer"> when clicking on that <span> tag rather than giving each <div> a different class ? 单击该<span>标记时,是否可以仅引用一个<div class=".answer">而不是给每个<div>一个不同的class Thanks! 谢谢!

The JavaScript code: JavaScript代码:

<script type="text/javascript">
    $(function() {
      $(".q_open").each(function() {
        $(this).click(function() {
          $(".answer").show();
        });
      })
      $(".q_close").each(function() {
        $(this).click(function() {
          $(".answer").hide();
        });
      });
    });
  </script>

The HTML code: HTML代码:

<h2 class="question">
          Q1: (the first question)
          <span class="q_open"></span>
          <span class="q_close"></span>
        </h2>
        <div class="answer" style="display: none;">
          This should only open when clicked by the <span class="q_open"> for Q1.
        </div>
        <h2 class="question">
          Q2: (the second question)
          <span class="q_open"></span>
          <span class="q_close"></span>
        </h2>
        <div class="answer" style="display: none;">
          <p>
           This should only open when clicked by the <span class="q_open"> for Q2.
          </p>
        </div>

I think you are looking for some sort of accordion. 我认为您正在寻找某种手风琴。 For that, you need to target the exact HTML element to open or close. 为此,您需要定位要打开或关闭的确切HTML元素。

Try this: 尝试这个:

$(function() {
  $(".q_open").click(function() {
    // Select the related target to open
    $(this).closest("h2").next(".answer").slideDown();
  });

  $(".q_close").click(function() {
    // Select the related target to close
    $(this).closest("h2").next(".answer").slideUp();
  });
});

Here is working example: https://jsfiddle.net/ashishanexpert/1xydbj22/ 这是工作示例: https : //jsfiddle.net/ashishanexpert/1xydbj22/

Use 采用

$(this).find('.answer').show()

Like this: 像这样:

<script type="text/javascript">
    $(function() {
      $(".q_open").each(function() {
        $(this).click(function() {
          $(this).find('.answer').show();
        });
      })
      $(".q_close").each(function() {
        $(this).click(function() {
          $(this).find('.answer').hide();
        });
      });
    });
  </script>

replace class with id 用ID替换类

 <script type="text/javascript">
 $(function() {
  $("#q1_open").click(function() {
      $("#answer1").show();
  });
 $("#q2_open").click(function() {
      $("#answer2").show();
  });
  $(".q_close").each(function() {
    $(this).click(function() {
      $("#answer1").hide();
      $("#answer2").hide();
    });
  });
});

Use closest() method to find first element traversing up through its ancestors in the DOM tree. 使用closest()方法在DOM树中查找遍历其祖先的第一个元素。

.next() will get the immediately following sibling of each element in the set of matched elements. .next()将获得匹配元素集中每个元素的紧随其后的同级元素。

 $('.q_open').click(function() { $(this).closest('.question').next(".answer").show(); }); $(".q_close").click(function() { $(this).closest('.question').next(".answer").hide(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <h2 class="question"> Q1: (the first question) <span class="q_open">Open</span> <span class="q_close">Close</span> </h2> <div class="answer" style="display: none;"> This should only open when clicked by the <span class="q_open"> for Q1.</span> </div> <h2 class="question"> Q2: (the second question) <span class="q_open">Open</span> <span class="q_close">Close</span> </h2> <div class="answer" style="display: none;"> <p> This should only open when clicked by the <span class="q_open"> for Q2.</span> </p> </div> 

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

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