2010-04-24 2 views
2

자, 여기에 내가하려는 중입니다. DISPLAY : NONE;으로 설정된 자식 요소가있는 DIV 상자가 있습니다. Jquery를 사용하여 마우스가 부모 DIV 상자에 들어갈 때 자식 요소가 보이게 한 다음 마우스가 부모 DV를 떠날 때 숨 깁니다. 이 클래스에는 페이지에 여러 div가 있습니다. 어떤 이유로 그것은 작동하지 않습니다. 어떤 아이디어?Jquery로 호버에 다른 클래스 디스플레이 변경

HTML :

<div class="parent"> 
    <span class="handle" style="display: none;">My Handle</span> 
    <p>Child Text</p> 
</div> 

자바 스크립트 : 대신

$(document).ready(function() { 
    $('.parent').mouseenter(function(){ 
     $(this).next('.handle').show(); 
    }); 
    $('.parent').mouseleave(function(){ 
     $(this).next('.handle').hide(); 
    }); 
}) 
+0

당신이 CSS 아래 유일한 해결책으로 간주 적이 있습니까? –

답변

3

사용 find : 여기 내 코드의 더 나은

$(document).ready(function() { 
    $('.parent').mouseenter(function(){ 
     $(this).find('.handle').show(); 
    }); 
    $('.parent').mouseleave(function(){ 
     $(this).find('.handle').hide(); 
    }); 
}) 

또는이 시도 :

$(document).ready(function() { 
    $('.parent').hover(function(){ 
     $('.handle', this).show(); 
    }, 
    function(){ 
     $('.handle', this).hide(); 
    }); 
    ); 
}) 
+0

그랬어! 정말 고마워! –

+0

@ John K : 환영합니다 .......... – Sarfraz

+0

요소 유형을 지정하는 것이 좋습니다. 쿼리 속도가 약간 빨라집니다. – ChaosPandion

0

hover을 사용하는 것이 좋습니다. 즉, 쿼리를 한 번 실행하면됩니다.

.parent .handle{ 
    display:none; 
} 
.parent:hover .handle{ 
    display:inline; 
} 

<div class="parent"> 
    <span class="handle">My Handle</span> 
    <p>Child Text</p> 
</div> 

를 그리고 자바 스크립트에 대한 필요성을 제거에만 있기 때문에 당신은 아마 CSS를 사용해야합니다

$('div.parent').hover(
    function() { 
     $(this).children('span.handle').show(); 
    }, 
    function() { 
     $(this).children('span.handle').hide(); 
    } 
); 
2

당신은 jQuery를하지 않고 목표를 달성 할 수있다.

는 FF 및 사파리

+0

이것은 작동하지 않습니다. 요소가 숨겨지면이를 발견 할 수 없었습니다. * Firefox에서만 테스트 *. – ChaosPandion

+0

내가 잘못 표시했기 때문입니다. 다시 시도하십시오. –

+0

정말 바보 같아. – ChaosPandion

0

대신,이 시도 테스트 :

$(document).ready(function() { 
    $('.parent').mouseenter(function(){ 
     $(this).next('.handle').show(); 
    }).mouseleave(function(){ 
     $(this).next('.handle').hide(); 
    }); 
}) 
관련 문제