How to add image to show/hide div javascript?

后端 未结 2 1326
梦毁少年i
梦毁少年i 2021-01-24 11:17

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\').         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-24 11:43

    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/

提交回复
热议问题