使用 jquery 更改 img src

发布于 2024-11-18 11:24:34 字数 793 浏览 8 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

栖竹 2024-11-25 11:24:34

您可以通过首先使用选择器选择所有图像,然后使用 attr 回调来 replace< 来替换每个 imgsrc /代码> 内容:

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

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");
})
晨与橙与城 2024-11-25 11:24:34

您可以为图像标签分配一个 id,就像

<img id ="pic" src="http://domain.com/directory/file3-128x79.jpg">

在 jquery 使用中一样

$('#pic').attr('src', 'file#-896x277.jpg');

you can assign an id to your image tag like

<img id ="pic" src="http://domain.com/directory/file3-128x79.jpg">

then in jquery use

$('#pic').attr('src', 'file#-896x277.jpg');
话少心凉 2024-11-25 11:24:34

演示

$('img').hover(function(){ // or any other method
    this.src = this.src.replace("128x79", "200x60");         
}); 

DEMO

$('img').hover(function(){ // or any other method
    this.src = this.src.replace("128x79", "200x60");         
}); 
非要怀念 2024-11-25 11:24:34

您应该在 .find('img') 之前添加 .children()

$('#something').removeAttr('id').attr('class', 'some-class').children().find('img').attr('src', 'none');

You should add .children() before .find('img'):

$('#something').removeAttr('id').attr('class', 'some-class').children().find('img').attr('src', 'none');
番薯 2024-11-25 11:24:34

注意:尝试以下鼠标悬停仅用于演示目的

$(function() {
    $("something li a img")
        .mouseover(function() { 
            var src = "over.gif";
            $(this).attr("src", src); // change the image source
        })

});

Note : try the following here mouse over is just for the demo purpose only

$(function() {
    $("something li a img")
        .mouseover(function() { 
            var src = "over.gif";
            $(this).attr("src", src); // change the image source
        })

});
红衣飘飘貌似仙 2024-11-25 11:24:34

使用 attr 怎么样:

this.removeAttr('id').prop('class', 'featured-images').find('img').attr({‘src’:'file#-896x277.jpg’});

How about using attr:

this.removeAttr('id').prop('class', 'featured-images').find('img').attr({‘src’:'file#-896x277.jpg’});
淡看悲欢离合 2024-11-25 11:24:34
$('#something img').attr('src',$('#something img').attr('src').replace(x,y))
$('#something img').attr('src',$('#something img').attr('src').replace(x,y))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文