[英]changing the img src with jquery
我有的html結構是這樣的:
<ul id="something">
<li>
<a href="">
<img src="http://domain.com/directory/file1-128x79.jpg">
</a>
</li>
<li>
<a href="">
<img src="http://domain.com/directory/file2-128x79.jpg">
</a>
</li>
<li>
<a href="">
<img src="http://domain.com/directory/file3-128x79.jpg">
</a>
</li>
</ul>
我正在嘗試將文件名從文件#-128x79.jpg更改為文件#-896x277.jpg 。
我不知道如何獲取動態生成的文件名並搜索和替換src更改。
我找到了用'none'替換整個src的方法,以確保我到目前為止做到了,但我不知道如何做其余的事情。
$('#something').removeAttr('id').prop('class', 'some-class').find('img').prop('src', 'none');
您可以通過首先使用選擇器選擇所有圖像,然后使用attr
回調replace
內容來replace
每個img
的src
:
$('#something img').attr('src',function(i,e){
return e.replace("-128x79.jpg","-896x277.jpg");
})
你可以為你的圖片標簽指定一個id
<img id ="pic" src="http://domain.com/directory/file3-128x79.jpg">
然后在jquery中使用
$('#pic').attr('src', 'file#-896x277.jpg');
$('img').hover(function(){ // or any other method
this.src = this.src.replace("128x79", "200x60");
});
您應該添加.children()
之前.find('img')
$('#something').removeAttr('id').attr('class', 'some-class').children().find('img').attr('src', 'none');
注意:請嘗試以下鼠標,僅用於演示目的
$(function() {
$("something li a img")
.mouseover(function() {
var src = "over.gif";
$(this).attr("src", src); // change the image source
})
});
使用attr怎么樣:
this.removeAttr('id').prop('class', 'featured-images').find('img').attr({‘src’:'file#-896x277.jpg’});
$('#something img').attr('src',$('#something img').attr('src').replace(x,y))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.