我正在尝试在地址开头的attr('src')
内更改字符串,并且在替换之后,它会保留后者的信息。
这是html:
<img src="../folder1/folder2/folder3/picture.png" />
<img src="../folder1/folder2/folder3/picture2.png" />
在页面加载后需要执行此操作,这是我现有的脚本/脚本:
$(document).ready(function(e) {
// this is one attempt
$('img').each(function() {
var src = $(this).attr('src');
$(this).ready(function() {
$(this).attr('src', src.replace('../', '/'));
}, function() {
$(this).attr('src', src);
});
});
// this is another attempt, not both used at the same time
$('img').attr('src').replace('../', '/');
// I also tried this
$('img').each(function() {
if($(this).attr('src') == '../') {
$(this).attr('src', '/');
};
});
});
总体目标是产生这样的结果:
<img src="/folder1/folder2/folder3/picture.png" />
<img src="/folder1/folder2/folder3/picture2.png" />
答案 0 :(得分:2)
您必须获取当前$('img').attr('src', function(index, src) {
return src.replace('../', '/')
})
- 更新它,然后重新分配它:
{{1}}