用jquery更改img src [英] changing the img src with jquery

查看:189
本文介绍了用jquery更改img src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有的html结构如下:

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>

我正在尝试从文件更改文件名#-128x79.jpg 文件#-896x277.jpg

我不知道如何获取动态生成的文件名并搜索和替换src更改。

I don't know how to take the dynamically generated filename and search and replace for the src changes.

我找到了用'none'替换整个src的方法,以确保我到目前为止做到了,但我不知道怎么做休息。

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');


推荐答案

你可以替换 src 每个 img 首先用选择器选择所有图像,然后使用 attr 回调替换内容:

You can replace the src for each img by first selecting all the images with a selector and then using the attr callback to replace the contents:

$('#something img').attr('src',function(i,e){
    return e.replace("-128x79.jpg","-896x277.jpg");
})

这篇关于用jquery更改img src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆