开发者

jQuery: contains() always returning true

I have a parent jQuery object and a child jQuery element.

I开发者_如何学Python'd like to see if the child is already contained within the parent. I was thinking of using jQuery's contains() method. However, in Chrome and IE I always get true returned and in FF6 I get an error a.compareDocumentPosition is not a function

Am I using this incorrectly? Is there a better way to achieve this?

Fiddle

Code:

<div class="metroContainer">
<div class="metroBigContainer">
    <div id="big1" class="metroBig">
        Stuffs 1
    </div>

    <div id="big2" class="metroBig">
        Stuffs 2
    </div>
</div>

<div class="otherContainer">
</div>

// I expect false, returns true
$.contains($('.metroBigContainer'), $('.otherContainer'))


I believe contains takes dom elements, not jquery objects:

$.contains($('.metroBigContainer')[0], $('.otherContainer')[0])


also you could try testing the length

$('.metroBigContainer .otherContainer').length

if it is 1 (or greater then 1) then it exists if not then it doesn't exist.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜