JQuery change the value of an <img

后端 未结 5 1649
旧时难觅i
旧时难觅i 2021-02-05 12:28

I need some simple JQuery code so I can change the src value of a specific img.

It\'s currently:


<         


        
相关标签:
5条回答
  • 2021-02-05 13:12
    $("#myImage").attr('src', 'image2.gif')
    
    0 讨论(0)
  • 2021-02-05 13:16

    You could use the .attr() function to set attributes on given DOM element:

    $(function() {
        $('#myImage').attr('src', 'image2.gif');
    });
    
    0 讨论(0)
  • $('#myImage').attr('src','theothersrc.gif')
    
    0 讨论(0)
  • 2021-02-05 13:23

    Using: $(function(){ ... });

    You can use:

    $('#id').attr('src', 'newImage.jpg');
    

    to change the image source immediately.


    Alternatively, you can use jQuery animations to change an image.

    JS

    $("#id1").fadeOut();
    $("#id2").delay(200).fadeIn();
    

    HTML

    <div>
        <img id='id1' src='one.jpg'>
        <img id='id2' src='two.jpg'>
    </div>
    

    (Don't forget to change the CSS of #id2 and put display: none as initial state).

    0 讨论(0)
  • 2021-02-05 13:24

    That's elementary, use jQuery attr...

    $('img#myImage').attr('src', 'image2.gif');
    
    0 讨论(0)
提交回复
热议问题