如何使用jQuery查找特定Div [英] How to find a particular Div using jQuery

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

问题描述

请指导我如何使用jQuery跟踪HTML来查找特定的div-

Please guide me how can I find a particular div using jQuery for following html -

       <div class="tab_container">
            <div id="tabs-1111" class="tab_content" style="display: block;">
                <p>26 High Street, Slough, SL1 1EP</p>
                <p></p>
            </div>
            <div id="tabs-1110" class="tab_content" style="display: none;">
                <p></p>
                <p></p>
                <div id="GoogleMap_Canvas" 
                    style="width: 410px; height: 450px; position: relative; overflow: hidden;">
                    <p></p>
                </div>
            </div>    
        </div>

我想检查具有'tab_content'类的第一个div是否包含id ='GoogleMap_Canvas'的div-与上面的html一样,对于第二个div来说应该是假的.

I want to check if first div with 'tab_content' class contains div with id = 'GoogleMap_Canvas' or not - which should come out to be false for above html as it is in second div.

我尝试使用-

if ($(".tab_content:first").children('#GoogleMap_Canvas').length > 0)
or
if ($(".tab_content:nth-child(1)").children('#GoogleMap_Canvas').length> 0)

这似乎对我来说很好.

但是,我很困惑为什么以下jQuery返回null?

However, I am confused why following jQuery returns null?

($(".tab_content")[1]).children('#GoogleMap_Canvas') //returns null

谢谢!

推荐答案

由于您要检查唯一的ID,因此不必那么复杂.

Since you are checking for a unique id, this doesn't need to be that complicated.

if ($('#GoogleMap_Canvas').length > 0)
  //map div exists

我相信您会得到null,因为您正在非jquery对象上调用.children.试试这个:

I believe you are getting a null because you are calling .children on a non-jquery object. Try this:

$($(".tab_content")[1]).children('#GoogleMap_Canvas')

或更好:

$(".tab_content:eq(1)").children('#GoogleMap_Canvas')

这篇关于如何使用jQuery查找特定Div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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