使用 jquery 更改 img src
我的 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>
我试图将文件名从 file#-128x79.jpg 更改为 file#-896x277.jpg。
我不知道如何获取动态生成的文件名并搜索和替换 src 更改。
我找到了用“none”替换整个 src 的方法,以确保到目前为止我做对了,但我不知道剩下的该怎么做。
$('#something').removeAttr('id').prop('class', 'some-class').find('img').prop('src', 'none');
The html structure I have is something like:
<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>
I'm trying to change the filename from file#-128x79.jpg to file#-896x277.jpg.
I don't know how to take the dynamically generated filename and search and replace for the src changes.
I found my way to replacing the whole src with 'none' to make sure I got it right so far, but I don't know how to do the rest.
$('#something').removeAttr('id').prop('class', 'some-class').find('img').prop('src', 'none');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以通过首先使用选择器选择所有图像,然后使用
attr
回调来replace< 来替换每个
img
的src
/代码> 内容:You can replace the
src
for eachimg
by first selecting all the images with a selector and then using theattr
callback toreplace
the contents:您可以为图像标签分配一个 id,就像
在 jquery 使用中一样
you can assign an id to your image tag like
then in jquery use
演示
DEMO
您应该在
.find('img')
之前添加.children()
:You should add
.children()
before.find('img')
:注意:尝试以下鼠标悬停仅用于演示目的
Note : try the following here mouse over is just for the demo purpose only
使用 attr 怎么样:
How about using attr: