jquery toggle with buttons two images?

2020-08-04 09:55发布

问题:

To toggle hide/show, i used this code;

togglebtn.click(function() {
    var that = this;
    $('.togglecontainer').toggle(500, function(){
        var txt = $(that).html();
        $(that).html((txt == 'Show less') ? 'Show more' : 'Show less');
    });
});
togglebtn.trigger("click");

But it toggles text inside one button. I want two buttons, with two diffrent img src's let's say images/show.png and images/hide.png

Thanks in advance

回答1:

Have a look at this Tutorial - jQuery Image Swap Using Click - This may be what you are trying to achieve. Let us know how you get on.



回答2:

I'm not really sure this is wat you mean but: You could use the Id's of the buttons

togglebtn.click(function(){
            var btn = this;
        $('.togglecontainer').toggle(500, function(){
            var txt = $(btn).html();
            $(btn).html((txt == 'Show less') ? 'Show more' : 'Show less');
            $("#btn2_Id").html((txt == 'Show less') ? '<img src="http://yourdomain.com/images/hide.png"/>' : '<img src="http://yourdomain.com/images/show.png"/>');
        });
});