Changing image on jquery toggle function

前提是你 提交于 2019-12-04 02:03:39

问题


I'm trying to change an image using the replace command when toggling using jquery..basically showing a downward arrrow which becomes an upward arrow after the content is revealed. This following code works except the downward arrow doesn't get replaced.

<img src="/images/arrow_down.png" id="#1" class="nav-toggle">
<div id="1" style="display:none">some content</div>

and here is the jquery code

jQuery('document').ready(function($) {
    jQuery('.nav-toggle').click(function() {
        var collapse_content_selector = $(this).attr('id');
        var toggle_switch = $(this);
        $(collapse_content_selector).toggle(function() {
            if ($(this).css('display') == 'none') {

            } else {
                toggle_switch.attr("src").replace("down", "up");
            }
        });
    });
});​

回答1:


You're not assigning it back to the src, try:

toggle_switch.attr("src", toggle_switch.attr("src").replace("down", "up"));



回答2:


Less code is better.

HTML

<a href="#" id="#arrow-toggle">
    <img src="/images/arrow_down.png">
    <img src="/images/arrow_up.png" class="up" style="display: none;">
</a>
<div id="content" style="display:none">some content</div>

jQuery

(function($) { 

    $('#arrow-toggle').click(function(event) {
        event.preventDefault();
        $('img:visible', this).hide().siblings().show();
        $('#content').toggle($('img:visible').is('.up'));
    });

})(jQuery);



回答3:


You should use something like;

jQuery('.nav-toggle').click(function() {
        var collapse_content_selector = $(this).attr('id');
        var toggle_switch = $(this);
        $(collapse_content_selector).toggle(function() {
            if ($(this).css('display') == 'none') {
                toggle_switch.attr("src", toggle_switch.attr("src").replace("image_2.jpg", "image_1.jpg"));
            } else {
                toggle_switch.attr("src", toggle_switch.attr("src").replace("image_1.jpg", "image_2.jpg"));
            }
        });
    });

Here is a live demo.




回答4:


I think you should try jQuery.UI.toggleSwitch()




回答5:


HTML

<img src="/images/arrow_down.png" id="#1" class="nav-toggle">
<div id="1" style="display:none">some content</div>

jQuery

$(function(){
  $('.nav-toggle').on('click', function(){
    var t = $(this);
    var imgSrc = t.attr('src');
    var divId = t.attr('id');
    $(divId).toggle('slow', function() {
      if (imgSrc === '/images/arrow_down.png') {
        t.attr({'src' : '/images/arrow_up.png'});
      } else {
        t.attr({'src' :'/images/arrow_down.png'});
      }
     });
  });
});

Try this one




回答6:


$("div#id").slideToggle(function() {
            if ($(this).css('display') == 'none') {
                $("div#id").css({'background-image':'url(css/img/nav_plus.gif)'});
            } else {
                $("div#id").css({'background-image':'url(css/img/nav_minus.gif)'});
            }
        });

another easy solution may be can help someone ;)



来源:https://stackoverflow.com/questions/12355799/changing-image-on-jquery-toggle-function

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