Jquery 如何选择父div内容

Jquery 如何选择父div内容,jquery,Jquery,我正试图用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> <

我正试图用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....

})
html
更多div..和所有动态生成的div。。
其他动态生成的图像。。。
第2组
第3组
第4组
第5组
更多的div。。。。
$('#btn')。单击(函数(){
//我必须使用parent()方法更改图像源。。。。
})

谢谢你的帮助

如果我理解正确,您希望能够更改特定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.


});

如果您的按钮可以有
ID

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

我通常使用.prev()和.find()来处理类似的事情。不确定这是最快还是最好的方法,但对我来说很有效

$("button").click(function(){               
    console.log($(this).parent("div").prev("div").find("img"));
});​

…更改什么图像的
href
,确切地说,其中有2个(或更多)图像。。。你不能简单地在保存图像的DIV中添加一个
id
class
吗?或者我想你可以直接去:$(this.parent(“DIV”).find(“DIV img”)
$("button").click(function(){               
    console.log($(this).parent("div").prev("div").find("img"));
});​