单击div显示/隐藏另一个

时间:2014-12-07 10:36:32

标签: javascript

我想要一个div,如果你点击它就会显示另一个div。 两个div都需要包含文本。我在网站上看到了这个,但我不知道该怎么做......

5 个答案:

答案 0 :(得分:1)

Omg ..你做过任何研究吗?

首先需要隐藏第二个div,其次,在第一个div中添加一个click功能以使其淡入...该死的...

$('#div2').hide();

或使用CSS

#div2{display:none;}

现在点击功能

$('#div1').click(function(){
   $('#div2').fadeIn();
});

答案 1 :(得分:1)

你去:

<div id="one">One</div>
<div id="two" style="display:none;">Two</div>
<script>
document.getElementById('one').addEventListener('click', function(){
    document.getElementById('two').style.display = 'block';
});
</script>
希望有所帮助。

答案 2 :(得分:1)

您可以通过向第一个div添加EventListener来实现此目的:

HTML:

<div id="one">firstdiv</div><div id="two" style="display:none">seconddiv</div>

JS:

var a = document.getElementById('one'); // get the element
var b = document.getElementById('two'); //  ---- " ----

a.addEventListener('click',showhide);   // listen on the event 'click' and call a function

function showhide () {
    if (b.style.display == 'none') {    // check if the element is visible
    b.style.display = 'block';          // if it's not visible, show it
    }
    else {
        b.style.display = 'none';       // else it's visible, hide it
    }
}

DEMO

答案 3 :(得分:0)

从评论中看,你需要一个手风琴。

我建议使用jQuery UI Accordian

jQuery UI Accordian的工作演示:

$(function() {
  $( "#accordion" ).accordion();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">

<div id="accordion">
  <h3>Section 1</h3>
  <div>
    <p>
      Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
      ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
      amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
      odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
    </p>
  </div>
  <h3>Section 2</h3>
  <div>
    <p>
      Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
      purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
      velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In
      suscipit faucibus urna.
    </p>
  </div>
  <h3>Section 3</h3>
  <div>
    <p>
      Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
      Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
      ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
      lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
    </p>
    <ul>
      <li>List item one</li>
      <li>List item two</li>
      <li>List item three</li>
    </ul>
  </div>
  <h3>Section 4</h3>
  <div>
    <p>
      Cras dictum. Pellentesque habitant morbi tristique senectus et netus
      et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in
      faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia
      mauris vel est.
    </p>
    <p>
      Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
      Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
      inceptos himenaeos.
    </p>
  </div>
</div>

答案 4 :(得分:-1)

jQuery就是你要找的。