2011-04-23 103 views
2

有谁知道如何去掉一部分url。去掉一部分url

例如我有3个img资源,每个资源都位于它自己的div类“showimage”中。

<div class="showimage"><img src="http://www.mydomain.com/image1-thumb.JPG" /></div> 
<div class="showimage"><img src="http://www.mydomain.com/image2-thumb.JPG" /></div> 
<div class="showimage"><img src="http://www.mydomain.com/image3-thumb.JPG" /></div> 

我想要所有的URL去除“-thumb”,这样img源码读取;

<div class="showimage"><img src="http://www.mydomain.com/image1.JPG" /></div> 
<div class="showimage"><img src="http://www.mydomain.com/image2.JPG" /></div> 
<div class="showimage"><img src="http://www.mydomain.com/image3.JPG" /></div> 

我只能访问CSS,JavaScript或jQuery的。

感谢您的帮助。

+0

如何:' “

” .replace( “ - 拇指”, “”)'或者你需要使用JS详细的解答? – Cipi 2011-04-23 07:31:55

+0

不,我需要它使用JS,因为我没有访问代码。感谢您的帮助! – user721521 2011-04-23 07:56:57

+0

这是JS ... xD – Cipi 2011-04-23 11:48:43

回答

0

你可以做这样的事情:

$(".showimage img").each(function() { 
    var imgSrc = $(this).attr("src"); 
    imgSrc = imgSrc.replace("-thumb",""); 
    $(this).attr("src",imgSrc); 
}); 
4
$("div.showimage img").attr("src",function(){ 
    return this.src.replace("-thumb",""); 
}); 
+0

@ user721521:这真的有用吗?因为代码实际上是错误的。它应该是'$(“div.showimage img”)。attr(“src”,function(i,src){return src.replace(“ - thumb”,“”);});'。 'this'引用当前元素,而不是'src'值。 – 2011-04-23 08:31:19

+0

@Felix Kling谢谢...我改变了它.. – 2011-04-23 08:45:07

+0

@Felix Kling之前的代码并没有工作,“像魅力一样工作”是为jSang下面。我在下面测试了你的代码和jSang,他们都工作。感谢大家! – user721521 2011-04-24 05:00:57