Changing the image source using jQuery

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

My DOM looks like this:

相关标签:
16条回答
  • 2020-11-22 01:58

    For more information. I try setting src attribute with attr method in jquery for ad image using the syntax for example: $("#myid").attr('src', '/images/sample.gif');

    This solution is useful and it works but if changing the path change also the path for image and not working.

    I've searching for resolve this issue but not found nothing.

    The solution is putting the '\' at the beginning the path: $("#myid").attr('src', '\images/sample.gif');

    This trick is very useful for me and I hope it is useful for other.

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

    Hope this can work

    <img id="dummyimage" src="http://dummyimage.com/450x255/" alt="" />
    <button id="changeSize">Change Size</button>
    
    $(document).ready(function() {
        var flag = 0;
        $("button#changeSize").click(function() {
            if (flag == 0) {
                $("#dummyimage").attr("src", "http://dummyimage.com/250x155/");
                flag = 1;
            } else if (flag == 1) {
                $("#dummyimage").attr("src", "http://dummyimage.com/450x255/");
                flag = 0;
            }
        });
    });
    
    0 讨论(0)
  • 2020-11-22 02:01

    You should add id attribute to your image tag, like this:

    <div id="d1">
       <div class="c1">
                <a href="#"><img id="img1" src="img1_on.gif"></a>
                <a href="#"><img id="img2" src="img2_on.gif"></a>
       </div>
    </div>
    

    then you can use this code to change the source of images:

     $(document).ready(function () {
            $("#img1").attr({ "src": "logo-ex-7.png" });
            $("#img2").attr({ "src": "logo-ex-8.png" });
        });
    
    0 讨论(0)
  • 2020-11-22 02:02

    This is a guaranteed way to get it done in Vanilla (or simply Pure) JavaScript:

    var picurl = 'pictures/apple.png';
    document.getElementById("image_id").src=picurl;
    
    0 讨论(0)
  • 2020-11-22 02:04

    Short but exact

    $("#d1 img").click(e=> e.target.src= pic[e.target.src.match(pic[0]) ? 1:0] );
    

    let pic=[
      "https://picsum.photos/id/237/40/40", // arbitrary - eg: "img1_on.gif",
      "https://picsum.photos/id/238/40/40", // arbitrary - eg: "img2_on.gif"
    ];
    
    $("#d1 img").click(e=> e.target.src= pic[e.target.src.match(pic[0]) ? 1:0] );
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/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>

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

    I had the same problem when trying to call re captcha button. After some searching, now the below function works fine in almost all the famous browsers(chrome,Firefox,IE,Edge,...):

    function recaptcha(theUrl) {
      $.get(theUrl, function(data, status){});
      $("#captcha-img").attr('src', "");
      setTimeout(function(){
           $("#captcha-img").attr('src', "captcha?"+new Date().getTime());
      }, 0);
     }
    

    'theUrl' is used to render new captcha image and can be ignored in your case. The most important point is generating new URL which forces FF and IE to rerender the image.

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