想要修改 img src 来自:
<div class="solo">
<img src="/uploads/2016/08/Simone-B.jpg?fit=97%2C146&ssl=1">
</div>
致:
<div class="solo">
<img src="/uploads/2016/08/Simone-B.jpg">
</div>
尝试使用以下但不起作用:
jQuery('.solo img').each(function(){
jQuery(this).attr('src',jQuery(this).attr('src').replace('?*',''));
});
有什么建议吗?提前致谢
最佳答案
在代码中 .replace('?*','')
替换字符串 ?*
。要删除该特定部分,您需要使用正则表达式,例如 .replace(/\?.*/,'')
。
但更好的方法是使用 attr()
方法,带有基于旧值进行迭代和更新的回调。您可以在哪里使用 String#split
删除属性值中?
后面的字符串部分的方法。
jQuery('.solo img').attr('src',function(i,v){
return v.split('?')[0]; // get the string part before the `?`
// or
// return v.replace(/\?.*/,'');
});
关于jQuery 删除 ? 之后的所有字符在img源文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39375360/