jQuery 删除 ? 之后的所有字符在 img src 中
jQuery remove all characters after ? in img src
想要修改 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()
method with a callback for iterate and update based on the old value. Where you can use String#split
方法去除属性值中?
之后的字符串部分。
jQuery('.solo img').attr('src',function(i,v){
return v.split('?')[0]; // get the string part before the `?`
// or
// return v.replace(/\?.*/,'');
});
想要修改 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()
method with a callback for iterate and update based on the old value. Where you can use String#split
方法去除属性值中?
之后的字符串部分。
jQuery('.solo img').attr('src',function(i,v){
return v.split('?')[0]; // get the string part before the `?`
// or
// return v.replace(/\?.*/,'');
});