Jquery子元素

时间:2010-03-14 15:40:19

标签: javascript jquery

当我使用query:

为某些元素添加事件处理程序时
$('div').mouseover(function () {

                                    });

Iinside函数我有一个元素,我们为其添加事件函数($(this))。 我如何在下面查看此功能:

  1. 拥有此“DIV”($(this))子元素 “DIV”?
  2. 有这个“DIV”($(this))孩子     元素“DIV”高度超过     300?

3 个答案:

答案 0 :(得分:1)

您可以将其放入mouseover事件代码中

$(this).children('div').each( function() { // $(this) is your parent <div>
  if ($(this).height() > 300) { // $(this) is the current child <div>
    // Do things here...
  }
});

答案 1 :(得分:0)

当你进入函数时,this指的是DIV元素。然后,您可以执行任何您希望了解的内容。

因此,要获得子DIV,您可以使用var childDivs = $('div',this);

答案 2 :(得分:0)

$('div').mouseover(function () {
   var children = $(this).children("div"); //for immediate child div
   if(children.length > 0){
     alert("'div' child present");  
     for(i=0; i < children.length; i++){
        if(children[i].height() > 300) 
           alert("'div' with height more than 300 present");
   } 

});

更新: 也可以使用children[i].css('height')