IF容器“x”不包含div“n”? JS

时间:2012-02-08 14:33:10

标签: javascript jquery

我想知道是否可以制作IF语句,如果容器(父div)包含/不包含某个div ....

<div class="parent" id="x">need to check if div id "n" is here....</div>

所以,IF父容器(“x”)不包含div“n”,然后插入它(我已经知道该怎么做)。

以上甚至可以实现吗?

JS和JQuery可以顺便使用..

由于

2 个答案:

答案 0 :(得分:3)

只需使用jQuery.contains

即可
var x = document.getElementById('x');
var n = document.getElementById('n');

if (jQuery.contains(x, n)) {
  // it does
}

答案 1 :(得分:3)

尝试这个

   $('#id:not(:has(#n))')