1

This might be easy but i get a problem,

How to test if the div comWF if present at least one time in comW ?

eg :

<div class="comW">
   <div class="comWF"></div>
</div>

if i make :

if($(".comWF").length>=1)) doSomething;

It test on the wall body not especially in each comW.

Thanks

4

6 に答える 6

3

Try this,

if($('.comW .comWF').length > 0)
{
   //comWF is present in comW
}

Update on request of OP, for all the html section satisfying the criteria.

$('.comW .comWF').each(function(){

    var comWFobj = $(this);
    var comWobj = $(this).closest('.comW');

    alert(comWFobj.attr('class'));
    alert(comWobj.attr('class'));

});
于 2012-09-17T15:55:57.703 に答える
3
var present = $('.comW .comWF').length > 0
于 2012-09-17T15:56:08.823 に答える
3

jQuery provides a dedicated static method for this task - $.contains():

if ( $.contains( divW, divWF ) ) {
    // do your thing
}

where divW, and divWF are DOM references to those DIVs.

Live demo: http://jsfiddle.net/8SadD/

于 2012-09-17T15:57:06.847 に答える
2
$('.comW').each(function(){
 var hasElem = $('.comWF', this).length;
 if( hasElem ){
    // do something
 }
});
于 2012-09-17T16:04:43.717 に答える
0

You can also try this

if ($('.comW').find('.comWF').length > 0){
// Put your code here
}

Thanks

于 2012-09-17T15:57:51.577 に答える
0

jsBin demo

if($('.comW').find('.comWF').length){
   // found!
}

or better:

$('.comW').each(function(){
  var $myChild = $(this).find('.comWF');
  if($myChild.length){
     // DO SOMETHING
  }
});
于 2012-09-17T16:03:03.720 に答える