使用javascript替换url

时间:2017-03-21 05:56:44

标签: javascript jquery

我的图片网址

<img src="http.://8.37.229.144:8999/wapka_img.php?pid=4668758&fid=52869690&type=4&size=240&filename=240.jpg/>

我想将网址更改为

<img src"http.://8.37.229.144:8999/wapka_img.php?pid=4668758&fid=52869690&type=4&size=120&filename=120.jpg"/>

你能帮助我吗

3 个答案:

答案 0 :(得分:1)

document.querySelector("img").forEach(function(elem) {
   elem.src = elem.src.replace("=240" , "=120");

});

答案 1 :(得分:0)

使用模板文字: - e.g: -

let url = `http.://8.37.229.144:8999/wapka_img.php?pid=4668758&fid=52869690&type=4&size=${size}&filename=${size}.jpg`;

答案 2 :(得分:0)

如果要更改页面中的某些IMG URL,可以使用jquery和regex来识别要更改的图像,并在给定模式中替换它们的URL。

var rx = /....../i; //The [regex][1] pattern for your URLs

//Iterate over all the images...
$('img')
    .filter(img=>rx.test(img.src)) //... filter those match the url requirement...
    .each((index,img)=>img.replace(rx,'[new_url_pattern]'); //...replace the URLs.
相关问题