Changing the image source using jQuery

前端 未结 16 1964
天命终不由人
天命终不由人 2020-11-22 01:24

My DOM looks like this:

相关标签:
16条回答
  • 2020-11-22 02:21

    One of the common mistakes people do when change the image source is not waiting for image load to do afterward actions like maturing image size etc. You will need to use jQuery .load() method to do stuff after image load.

    $('yourimageselector').attr('src', 'newsrc').load(function(){
        this.width;   // Note: $(this).width() will not work for in memory images
    
    });
    

    Reason for editing: https://stackoverflow.com/a/670433/561545

    0 讨论(0)
  • 2020-11-22 02:21

    Change the image source using jQuery click()

    element:

        <img class="letstalk btn"  src="images/chatbuble.png" />
    

    code:

        $(".letstalk").click(function(){
            var newsrc;
            if($(this).attr("src")=="/images/chatbuble.png")
            {
                newsrc="/images/closechat.png";
                $(this).attr("src", newsrc);
            }
            else
            {
                newsrc="/images/chatbuble.png";
                $(this).attr("src", newsrc);
            }
        });
    
    0 讨论(0)
  • 2020-11-22 02:22

    You can use jQuery's attr() function. For example, if your img tag has an id attribute of 'my_image', you would do this:

    <img id="my_image" src="first.jpg"/>
    

    Then you can change the src of your image with jQuery like this:

    $("#my_image").attr("src","second.jpg");
    

    To attach this to a click event, you could write:

    $('#my_image').on({
        'click': function(){
            $('#my_image').attr('src','second.jpg');
        }
    });
    

    To rotate the image, you could do this:

    $('img').on({
        'click': function() {
             var src = ($(this).attr('src') === 'img1_on.jpg')
                ? 'img2_on.jpg'
                : 'img1_on.jpg';
             $(this).attr('src', src);
        }
    });
    
    0 讨论(0)
  • 2020-11-22 02:22

    There is no way of changing the image source with CSS.

    Only possible way is using Javascript or any Javascript library like jQuery.

    Logic-

    The images are inside a div and there are no class or id with that image.

    So logic will be select the elements inside the div where the images are located.

    Then select all the images elements with loop and change the image src with Javascript / jQuery.

    Example Code with demo output-

    $(document).ready(function()
    {
        $("button").click(function()
        {
          $("#d1 .c1 a").each(function()
          {
              $(this).children('img').attr('src', 'https://www.gravatar.com/avatar/e56672acdbce5d9eda58a178ade59ffe');
          });
        });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    
    <div id="d1">
       <div class="c1">
                <a href="#"><img src="img1_on.gif"></a>
                <a href="#"><img src="img2_on.gif"></a>
       </div>
    </div>
    
    <button>Change The Images</button>

    0 讨论(0)
提交回复
热议问题