15

私が持っている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 の変更を検索および置換する方法がわかりません。

src全体を「none」に置き換えて、これまでのところ正しいことを確認する方法を見つけましたが、残りの方法がわかりません。

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

7 に答える 7

17

最初にすべての画像をセレクターで選択し、次にコンテンツへのコールバックを使用して、 srcfor eachを置き換えることができます。imgattrreplace

$('#something img').attr('src',function(i,e){
    return e.replace("-128x79.jpg","-896x277.jpg");
})
于 2011-07-04T07:23:48.107 に答える
6

次のようにイメージタグにIDを割り当てることができます

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

次にjqueryで使用

$('#pic').attr('src', 'file#-896x277.jpg');
于 2011-07-04T07:21:49.727 に答える
3

デモ

$('img').hover(function(){ // or any other method
    this.src = this.src.replace("128x79", "200x60");         
}); 
于 2011-07-04T07:36:07.710 に答える
1

.children()前に追加する必要があります.find('img')

$('#something').removeAttr('id').attr('class', 'some-class').children().find('img').attr('src', 'none');
于 2011-07-04T07:21:44.353 に答える
1

注:ここで次のことを試してください。マウスオーバーはデモ目的のみです

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

});
于 2011-07-04T07:22:12.413 に答える
0
$('#something img').attr('src',$('#something img').attr('src').replace(x,y))
于 2011-07-04T07:26:34.377 に答える
0

attrを使用するのはどうですか:

this.removeAttr('id').prop('class', 'featured-images').find('img').attr({‘src’:'file#-896x277.jpg’});
于 2011-07-04T07:22:35.597 に答える