Hide/show toggle separate divs with the same class name

試著忘記壹切 提交于 2020-01-15 10:39:33

问题


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;
  });

});

回答1:


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:

  • closest().
  • find().



回答2:


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>



回答3:


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();
});



回答4:


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:


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;
  });

});



回答6:


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; }




回答7:


Try this:

$(window).load(function() {

var $jobs = $('.job_description');
$('.grey_button a').click(function() {
    $closest = $(this).closest('li').find('.job_description');
    $jobs.not($closest).slideUp();
    $closest.slideDown();
    return false;
});

});



回答8:


check out this jsfiddle example here.

for a glance of jquery fuction ...try :

$('.top').on('click', function() {
    $parent_box = $(this).closest('.box');
    $parent_box.siblings().find('.bottom').hide();
    $parent_box.find('.bottom').toggle();
});


来源:https://stackoverflow.com/questions/10444694/hide-show-toggle-separate-divs-with-the-same-class-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!