Replace img path jquery

前端 未结 6 1022
后悔当初
后悔当初 2021-01-26 18:48

I am trying to replace an img path in jquery (injecting into a remote page)

replace example.com/thumbs

with ex

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-26 19:36

    This gets the value, but doesn't set it back to the attribute:

    $("img").attr("src").replace("thumbs", "images");
    

    That requires another step, something like:

    var newSrc = $("img").attr("src").replace("thumbs", "images");
    $("img").attr("src", newSrc);
    

    Or, if you want a single line:

    $("img").attr("src", $("img").attr("src").replace("thumbs", "images"));
    

提交回复
热议问题