2010-03-14 7 views
0

:JQuery와 자식 요소

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

            }); 

Iinside 기능은 우리가 이벤트 기능을 추가하는 요소가 ($ (이)). 는 어떻게 다음이 함수 내에서 확인할 수 있습니다

  1. 이 "DIV"가 ($ (이)) 하위 요소를 "DIV"를?
  2. 이 "DIV"($ (this)) 아동 요소 "DIV"whith의 높이가 300 이상입니까?
+2

은 수정하십시오. 네가 무엇을 요구하는지 모르겠다. –

+0

btw, 위대한 관련 스 니펫을 많이 보시려면 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... 
    } 
});