Hide/show toggle separate divs with the same class

2019-07-30 09:27发布

I've got a UL list, each LI has a hidden DIV, and a "More info" link that shows the hidden DIV. However, clicking this button shows all the other LI's hidden DIVs as well.

How can I only hide/show the DIV in the LI, and not have all the other hidden DIV's show?

And if I click on one how can I hide the others? I'd like to keep this part separate though in case I want to remove it later.

Also on click I want the text in the "More info" link to change to "Hide".

Here's my current script:

$(window).load(function() {

$('.grey_button a').toggle(function() {
    $('.job_description').slideDown('');
    return false;
  },
    function() {
      $('.job_description').slideUp('');
    return false;
  });

});

8条回答
Anthone
2楼-- · 2019-07-30 09:39

if html structure is like this ::

<ul class="main">
   <li>
        <p class="important-info">Bala bala</p>
        <p class="more-info" style="display:none;">More bla bla</p>
        <a class="_show-more-info">More Information</a>
   </li>
   <li>
        <p class="important-info">Bala bala</p>
        <p class="more-info" style="display:none;">More bla bla</p>
        <a class="_show-more-info">More Information</a>
   </li>
<ul>

then Jquery Code for your requirement will be like this

$('_show-more-info').click(function(){
    var thisAnchor = $(this);
    var ul = thisAnchor.parents('.main');
    ul.find('.more-info').slideUp();
    thisAnchor.sibling('.more-info').slideDown();
});
查看更多
神经病院院长
3楼-- · 2019-07-30 09:40

Jquery:

      $('.grey_button a').click(function() {
           $(this).closest('li').find('.job_description').slideToggle();

       });

CSS make job-information hidden initially:

     <ul>
       <li><span class="grey_button"><a href="#">Show more information</a></span>
      <div class="job_description" style="display:none" >Job information...</div></li>

    </ul>
查看更多
我想做一个坏孩纸
4楼-- · 2019-07-30 09:43

This code will open the actual content and hide all others:

<style type="text/css">
.job_description { display: none; }

.grey_button.close span.hide,
.grey_button.open span.more { display: none; }

.grey_button.close span.more,
.grey_button.open span.hide { display: inline; }​
</style>
<script type="text/javascript" encoding="utf-8">
$(document).ready(function() {
    $('.grey_button.close').live('click', function() {
        $('.grey_button.open').click();
        $('.job_description').slideUp();
        $(this).siblings('.job_description').slideDown();
        $(this).toggleClass('close open');
        return false;
    });
    $('.grey_button.open').live('click', function() {
        $('.job_description').slideUp();
        $(this).toggleClass('close open');
        return false;
    });
});
</script>
<ul>
<li>short description
    <a class="grey_button close" href="#">
        <span class="more">read more</span>
        <span class="hide">hide</span></a>
    <div class="job_description">Here is more to read.</div>
</li>
<li>short description 
    <a class="grey_button close" href="#">
        <span class="more">read more</span>
        <span class="hide">hide</span></a>
    <div class="job_description">Here is more to read.</div>
</li>
<li>short description 
    <a class="grey_button close" href="#">
        <span class="more">read more</span>
        <span class="hide">hide</span></a>
    <div class="job_description">Here is more to read.</div>
</li>
<li>short description 
    <a class="grey_button close" href="#">
        <span class="more">read more</span>
        <span class="hide">hide</span></a>
    <div class="job_description">Here is more to read.</div>
</li>
</ul>​
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-07-30 09:48

calling .slideUp() on the class will hide them all, and then just use the $(this) keyword to trigger only the class within the li.

try:

$('.grey_button a').toggle(function() { $('.job_description').slideUp(); $(this).parent().find('.job_description').slideDown(); return false; }

查看更多
狗以群分
6楼-- · 2019-07-30 09:49

You should use this context while accessing the li Please try

$(window).load(function() {

$('.grey_button a').toggle(function() {
    //hide the other 
    $('.grey_button a').not($(this)).find('.job_description').each(function(){
         $(this).slideUp();
    })

    $('.job_description',this).slideDown('');
    return false;
  },
    function() {
      $('.job_description',this).slideUp('');
    return false;
  });

});
查看更多
可以哭但决不认输i
7楼-- · 2019-07-30 09:51

The following jQuery should work:

$('.grey_button a').toggle(function() {
    $(this).closest('li').find('.job_description').slideDown();
    return false;
  },
    function() {
      $(this).closest('li').find('.job_description').slideUp();
    return false;
  });

This assumes HTML similar to the following:

<ul>
    <li><span class="grey_button"><a href="#">Show more information</a></span>
        <div class="job_description">Job information...</div></li>
    <!-- other list items... -->
</ul>

JS Fiddle demo.

Incidentally, there's no need to pass an empty string to slideUp()/slideDown(), without an argument being passed (either an integer (number in millisecons), or a string) a default value will be used instead, of 400 milliseconds.

References:

查看更多
登录 后发表回答