请帮帮我 如何替换 s120 中的字符串 url 图像
s120/demo%2Bproduct%2Blipstik.jpg
至
s400/demo%2Bproduct%2Blipstik.jpg
//jQuery
$(document).ready(function(){
$("#largeimage").click(function(){
$(".container img").attr("src").replace("s120","s400");
});
});
html
<div class="container center">
<img src="https://2.bp.blogspot.com/-NwKHFcL48ZA/WJ2FANm2TXI/AAAAAAAAA6Y/hE-85KroGacBYy26mnEbkEGYFcnyWWv4gCLcB/s120/demo%2Bproduct%2Blipstik.jpg">
</div>
<p>
<button class="center" id="largeimage">Large Image to 400</button>
</p>
最佳答案
您没有更新属性,请使用 attr()
的回调来执行此操作方法将旧值作为第二个参数。
$(document).ready(function() {
$("#largeimage").click(function() {
$(".container img").attr("src", function(i, src) {
return src.replace("s120", "s400");
});
});
});
关于javascript - jquery替换url图像中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42460153/