2010-03-14 63 views
0

當我的事件處理程序添加到使用查詢一些元素:jQuery的子元素

$('div').mouseover(function() { 

            }); 

Iinside功能我爲我們添加事件功能的元素($(本))。 我怎麼能檢查接下來的這個函數內部:

  1. 有這種 「DIV」($(本))的子元素 「DIV」?
  2. 有這個「DIV」($(this))的孩子 元素「DIV」的高度超過 300?
+2

請修改。不確定你在問什麼。 –

+0

順便說一句,看看這裏的很多偉大的相關片段http://addyosmani.com/blog/50-jquery-snippets-for-developers/ – adardesign

回答

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')也可以使用。

+0

但是在兒童[i] .height()有一個錯誤:它僅適用於語法$(children [i])。height() – Anton

+0

@Anton:Hmm。在這種情況下,你可以使用'.css('height')'。 –

0

當您在功能中時,this引用DIV元素。然後你可以做任何你想了解更多的信息。

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

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... 
    } 
});