2017-12-18 23 views
-2

나는 mouseleave 이벤트가 있습니다. 내가 요소를 떠날 때, 나는 그 요소를 제거하지 않을 것입니다 (이것이 감각이라면). 아래 예제와 내가 시도한 것. 나는 그것이 그렇게 작동하지 않는다고 생각한다. (나는 짐작할 만하다.) 그러나 그것을하기위한 튼튼한 방법은 무엇인가? ATM.leaved의 요소를 제거하십시오

$('#wrapper').remove() 

오히려

+0

귀하의 코드가 단지 조각, 당신은'당신이 당신의 처음 두 섹션에 나열했습니다 .remove()를'사용할 때 작동하지만' .remove'. – Herohtar

답변

1

remove()

$(document).on('mouseleave', '#wrapper', function(e) { 
 
    console.log($(this).remove); 
 
    alert('Mouse leaved and wrapper should be removed'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="wrapper">asdcasdcasd</div>

$(document).on('mouseleave', '#wrapper', function(e) { 
    console.log($(this).remove()); 
    alert('Mouse leaved and wrapper should be removed'); 
}); 

보다 방법이 아니다 속성입니다 : 나는 같은 선택을 사용할 수 없습니다. 사용 remove()하지 remove

$(document).on('mouseleave', '#wrapper', function(e) { 
 
    console.log($(this).remove()); 
 
    alert('Mouse leaved and wrapper should be removed'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="wrapper">asdcasdcasd</div>

+0

필자의 경우에는 # 래퍼가 추가 된 것과 관련이 있어야합니다. 어쨌든 나는 당신의 대답을 받아 들일 것입니다, 그것은 remove()없이 타이포 이유입니다 :). – simon

+0

@simon, 코드에서 무엇을 달성하기를 원하십니까? 이벤트는'# wrapper' 엘리먼트에 바인딩되어'this '가'# wrapper'로 참조됩니다. – Mamun