用jquery更改img src
我有的html结构是这样的:
我正在尝试将文件名从文件#-128×79.jpg更改为文件#-896×277.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
然后在jquery中使用
$('#pic').attr('src', 'file#-896x277.jpg');
DEMO
$('img').hover(function(){ // or any other method this.src = this.src.replace("128x79", "200x60"); });
你应该在.find('img')
之前添加.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))