-3

Jqueryを使用して親divのサイドブリング内の画像を見つけようとしています。それを見つけるための最良かつ迅速な方法は何だろうか。

html

more divs..and all generated dynamically..
<div>
other dynamically generated image...

<img src='haha1.jpg' />
<img src='haha2.jpg' />
</div>

<div>div 2</div>
<div>div 3</div>
<div>div 4</div>
<div>div 5</div>

more divs....

<div>
<button id='btn'/>
</div>


$('#btn').click(function(){

   //I have to change image source by using parent() method....

})

助けてくれてありがとう!

4

3 に答える 3

0

私があなたを正しく理解しているなら、あなたは特定のdiv内の画像のsrcを変更できるようにしたいと思っています。その場合は、このコードのオプションが役立つかどうかを確認してください。

$('#btn').click(function(){

//I have to change image source by using parent() method....


$("#divId").children("img").attr("src", "newImgSource.jpg"); //this will probably  change all the images int the div.
$("#imgId").attr("src", "newImgSource.jpg");//this should change a specific image's src.


});
于 2012-09-06T15:50:20.163 に答える
0

あなたのボタンが持つことができるならID...

<div id="images">  <!-- add an ID here too -->
   <img src='haha1.jpg' />
   <img src='haha2.jpg' />
</div>

jQuery:

var source = 'folder_name/';

$('#btn').click(function(){
   $('#images').find('img').each(function(){
      $(this).attr('src', source+this.src);
   });
});
于 2012-09-06T15:51:39.090 に答える
0

http://jsfiddle.net/truuD/12/

私は通常、このようなことのために.prev()と.find()を使用します。それが最速か最良の方法かはわかりませんが、私にとってはうまくいきます。

$("button").click(function(){               
    console.log($(this).parent("div").prev("div").find("img"));
});​
于 2012-09-06T15:51:48.543 に答える