2012-05-23 2 views
1

mouseenter에 요소를 추가하려고 시도한 다음 mouseleave에서 동일한 요소를 제거하려고합니다.추가 다음 jQuery를 사용하여 요소 제거

$(#foo).mouseenter(function(){ 
    $(this).append(#bar); 
}); 
$(#foo).mouseleave(function(){ 
    $(this).remove(#bar); 
}); 

내가 잘못 했나요?

+0

당신은 당신의 선택기를 인용 할 필요가 있습니까? – FatalError

답변

2

인용 부호가없는 문자열 리터럴은 가장 명백한 것으로 보입니다. 당신은 시도 :

$("#foo").mouseenter(function(){ 
    $(this).append("#bar"); 
}); 
$("#foo").mouseleave(function(){ 
    $(this).remove("#bar"); 
}); 

jQuery의 요소 - 선택 기능 (즉, $()는) ".someClass" 또는 "#someId" 또는 "div span.label" 같은 CSS 스타일 선택기를 포함하는 문자열을 기대하고있다. 따옴표를 사용하지 않고 이러한 것들을 전달하는 것은 구문 오류입니다.

+0

고마워, 그게 문제 야, 너 락. – dcd0181

2
​$("#foo").on("mouseenter mouseleave", function(e){ 
    e.type === "mouseenter" 
     ? $("#bar").appendTo(this) 
     : $("#bar", this).remove() ; 
});​ 

피들러 : http://jsfiddle.net/AzEnm/1/

관련 문제