jQuery在Parent中查找Div

我不确定我的措辞是否正确。 但我有一个类似于此的HTML文档设置。

 

如果有人点击car_story li我正试图定位page_text div。 但是因为有两个具有page_text div,我不知道如何定位与car_story ul在同一父div中的div。 希望这是有道理的。 谢谢!

我尝试过类似的东西,但没有运气。

 $(".car_story li").click(function() { $(this).parent().find(".page_text img").attr("src","newimage.jpg"); }); 

将最近的div.sub_page作为选择器中的上下文传递,然后传递要查找的元素

 $(".car_story li").click(function() { var parent = $(this).closest('.sub_page'); // find closest sub_page $(".page_text img",parent).attr("src","newimage.jpg"); // find img inside of closest sub_page }); 
 $(".car_story li").click(function() { $(this).parents("div").find(".page_text > img").attr("src","newimage.jpg"); }); 

试试closest()

 $(".car_story li").click(function() { $(this).closest('.sub_page').find(".page_text img") .attr("src","newimage.jpg"); }); 

这有相同的结果,但我认为更清楚的是,在所有情况下你会得到什么结果(例如,如果你有另一个页面可能被ajaxed也碰巧与car_story类有一个li然后这个点击事件将仍然只为spark_page开火。):

 $(".car_story li").click(function() { $('div#spark_page').find(".page_text img").attr("src","newimage.jpg"); }); 

当然,如果你使用li car_story更多的html进行ajax并希望更改所有.page_text img的源代码,那么我认为你可以做到

 $(".car_story li").click(function() { $(".page_text img").attr("src","newimage.jpg"); });