开发者

How to add image to show/hide div javascript?

开发者 https://www.devze.com 2023-03-28 03:56 出处:网络
Any suggestions how to add image (like + or - ) to show/hide div in javascript? I\'m using this code to show/hide divs:

Any suggestions how to add image (like + or - ) to show/hide div in javascript?

I'm using this code to show/hide divs:

$(function() {
    $('a.hide').click(function() {
        $(this).closest('.hideable').find('.hide-container').toggle();
    });
    $('a#hide-all').click(function() {
        $('.hide-container').hide();
    });
    $('.hide-container').hide();
    $('a.hide').c开发者_开发百科lick(function(e) {
        e.preventDefault();
    });
});

How can I add indicator images?


this is just pseudo code:

$(image).click(function(){
   $(div).toggle('fast',function(){
      if($(div).is(":visible"))$(image).attr('src','minus-image');
      else $(image).attr('src','plus-image');
   });
});


Set the image as a background in your css:

.hideable a.hide {
    background-image: url(minus.png);
    background-repeat: no-repeat;
    padding-left: 12px;
}
.hidden a.hide {
    background-image: url(plus.png);
}
.hidden .hide-container {
    display: none;
}

Then, use .toggleClass() instead of .toggle(). Apply the class to the parent element:

$('a.hide').click(function(e) {
    e.preventDefault();
    $(this).closest('.hideable').toggleClass("hidden");
});
$('a#hide-all').click(function() {
    $('.hideable').addClass("hidden");
});
$('.hideable').addClass("hidden");

Working Demo: http://jsfiddle.net/gilly3/rK7yN/1/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号