jQuery在父级中查找Div [英] jQuery Find Div within Parent

查看:101
本文介绍了jQuery在父级中查找Div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我的措词是否正确.但是我有一个HTML文档设置与此类似.

I'm not sure I worded this correctly. But I have an HTML document setup something similar to this.

<div id="intro_page" class="sub_page">

        <div class="sub_header"><img src="assets/img/intro_header.jpg" /></div>

        <div class="video_holder">
            <video class="video_player"></video>
        </div>

        <div class="page_text"><img src="" width="1020" /></div>


</div><!-- /intro_page -->

<div id="spark_page" class="sub_page car_page">

            <div class="sub_header"><img src="assets/img/header.jpg" /></div>

            <div class="video_holder">
                <video class="video_player"></video>
            </div>

            <ul class="car_story">
                <li data-text="story1_text"><img src="assets/img/story1_btn2.jpg" /></li>
                <li><img src="assets/img/story2_btn2.jpg" /></li>
                <li><img src="assets/img/story3_btn2.jpg" /></li>
            </ul>


            <div class="page_text"><img src="" width="1020" /></div>

</div><!-- /spark_page -->

如果有人点击car_story li,我将尝试定位page_text div.但是因为有两个具有page_text的div,所以我不知道如何定位与car_story ul处于同一父div中的一个.希望这是有道理的.谢谢!

I'm trying to target the page_text div if someone clicks on a car_story li. But because there are two divs that have page_text, I don't know how to target the one which is in the same parent div as the car_story ul. Hope this makes sense. Thanks!

我已经尝试过类似的操作,但是没有运气.

I've tried things similar to this, but with no luck.

$(".car_story li").click(function() {

    $(this).parent().find(".page_text img").attr("src","newimage.jpg");

});

推荐答案

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

Pass in the closest div.sub_page as the context in the selector, then the element you want to find

$(".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
});

这篇关于jQuery在父级中查找Div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆