How to set the image src using jQuery

前端 未结 4 733
半阙折子戏
半阙折子戏 2020-12-29 01:21

I am trying to change the image src attribute using jQuery

jQuery(\"#imageID\").attr(\'src\',\'http://localhost:8080/images/1/myImage.png\' );
4条回答
  •  余生分开走
    2020-12-29 02:01

    You need to extract the url part:

    var backgroundImage = $("#imageBlock")
        .css('backgroundImage')
        .replace(/"/g,"")
        .replace(/url\(|\)$/ig, "");
    jQuery("#imageID").attr('src', backgroundImage);
    

提交回复
热议问题