在jquery中的所有src标记上添加value属性

时间:2011-05-14 19:09:51

标签: jquery html tags src

我可以使用jquery在当前html页面的所有src标签上添加一些值属性吗? 例如这个html:

<img src="images/slider/slide4.jpg" />

是这样的:

<img src="http://www.website.com/images/slider/slide4.jpg" />

谢谢你们

2 个答案:

答案 0 :(得分:3)

$('img').each(function() {
    var newSrc = 'http://www.website.com/' + $(this).attr('src');
    $(this).attr('src', newSrc);
});

编辑:您不需要each循环,忽略以前的代码(保留以供参考); attr函数已经为你做了!

$('img').attr('src', function() {
    return 'http://www.website.com/' + this.src;
});

请参阅here中的最后一个示例。

答案 1 :(得分:0)

不确定是否有一些最短路。

$('img').each(function() {
    $(this).attr('src', some_string + $(this).attr('src'));
});