如何检查Selection对象是否包含两个兄弟'Div'或'P'

时间:2015-04-23 05:06:12

标签: javascript jquery dom selection siblings

我想检查用户选择中是否有任何两个或更多div或段兄弟的条件。 我突然想到这是对dom东西的新手。任何人都遇到过这样的情况,或者有人可以帮助我使用算法或逻辑来实现这一目标。任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:1)

Whatever you asked here, please share code snippet. Here i make for you example to achieve that. Since you did't post any code, then i'm not sure the example provided meet the expectations.

HTML

<div id="a">Click me
   <p></p>
   <div></div>
   <div></div>    
</div>

JS

$('#a').on('click', function(){
  var div = $(this).children('div').length;
  var p = $(this).children('p').length;

  if(div >= 2)
  {
    alert('div exists : ' + div);
    //do your stuff here
 }
 else
 {
    alert('i have div below than two');
    //do your stuff here if below than two
 }

//same goes with p
});

DEMO