我正在尝试使用Jquery代码,该代码可以找到页面中所有src
标记的<img>
,并将其替换并附加到https://s3.amazonaws.com/abc,例如
<img src="/v/abx/templates/210/images/clear1x1.gif" width="5" height="5" alt="" />
应替换为
<img src="https://s3.amazonaws.com/abc/v/abx/templates/210/images/clear1x1.gif" width="5" height="5" alt="" />
主要问题是它应该在页面加载
上发生我有以下Javascript代码,但它不起作用
<!-- START: javascript to manipulate product photo URLs -->
<script type="text/javascript">
if(location.href.indexOf(‘/product_p/’) != -1) {
var ThisPhoto=document.getElementById('product_photo').src;
var ImgServer='https://s3.amazonaws.com/abc';
var ThisDomain=document.domain;
var NewImgSrc = ThisPhoto.replace(ThisDomain,ImgServer);
document.getElementById('product_photo').src=NewImgSrc;
}
</script>
<!-- END: javascript to manipulate product photo URLs -->
答案 0 :(得分:4)
$('img').each(function(){
$(this).attr('src','https://s3.amazonaws.com/abc/'+$(this).attr('src'));
});
将其放在$(document).ready
处理程序中。