jQuery에서 "hover"바인딩을 해제하려면 어떻게합니까?


107

jQuery에서 "hover"를 어떻게 해제합니까?

작동하지 않습니다.

$(this).unbind('hover');

2
hover 이벤트에 할당 한 기능의 바인딩을 해제하려고하거나 <a> </a> hover를 수정하려고합니까?
Justin Niessner 2009

Justin Niessner의 질문을 명확히하기 위해 Javascript / DOM 이벤트 또는 CSS 선언을 제거하려고합니까? 후자는 더 복잡한 문제입니다.
눈꺼풀 없음

답변:


214

$(this).unbind('mouseenter').unbind('mouseleave')

또는 더 간결하게 ( @Chad Grant에게 감사드립니다 ) :

$(this).unbind('mouseenter mouseleave')


42
또는 (이) .unbind ( 'mouseenter하는 MouseLeave') $
차드 그랜트

mouseleave 후 mouseenter에 필요한 시퀀스입니까?
sanghavi7

70

실제로 jQuery 문서 는 위에 표시된 연결 예제보다 더 간단한 접근 방식을 가지고 있습니다 (물론 잘 작동하지만).

$("#myElement").unbind('mouseenter mouseleave');

jQuery 1.7부터 $.on()$.off()이벤트 바인딩 에도 사용할 수 있으므로 hover 이벤트의 바인딩을 해제하려면 더 간단하고 깔끔한 방법을 사용합니다.

$('#myElement').off('hover');

의사 이벤트 이름 "hover" "mouseenter mouseleave" 의 약어로 사용 되지만 이전 jQuery 버전에서는 다르게 처리되었습니다. 각 리터럴 이벤트 이름을 명시 적으로 제거해야합니다. 사용$.off() 지금은 같은 속기를 사용하여 두 마우스 이벤트를 제거 할 수 있습니다.

2016 년 수정 :

여전히 인기있는 질문이므로 jQuery 1.9+에서 "hover"이벤트가 표준 "mouseenter mouseleave"호출에 찬성하여 더 이상 사용되지 않는다는 아래 주석의 @ Dennis98 의 요점에 주목할 가치 가 있습니다. 따라서 이벤트 바인딩 선언은 다음과 같아야합니다.

$('#myElement').off('mouseenter mouseleave');


4
나는 jQuery 1.10.2가 있고 $.off("hover")작동하지 않습니다. 그러나 두 이벤트를 모두 사용하면 효과적입니다.
Alexis Wilke 2014 년

여러 필터링 인수가 제공되는 경우 제공된 모든 인수가 이벤트 핸들러를 제거하려면 일치해야한다는 점을 언급 할 가치가 있습니다. 또한 jQuery API 문서에는 .off () 메서드가 .on ()으로 연결된 이벤트 처리기를 제거한다고 명시되어 있습니다. 그것이 .on ()을 사용하여 추가 된 이벤트에만 적용되는지 여부는 확실하지 않습니다. 그러나 그렇게해서는 안됩니다.
Phil.Wheeler 2015 년

@AlexisWilke 네, v1.9에서 제거되었습니다. 마지막 링크를 찾아보십시오.;)
Dennis98

1
@ Dennis98-deprecated.js로 이동되는 jQuery 호버 해킹에 대해 이야기하고 있습니까? $ .off () 이벤트 바인딩은 이후 버전의 jQuery에서 계속 사용할 수 있습니다.
Phil.Wheeler

권리. :) $.off()여전히 존재합니다. 현재 권장되는 이벤트 바인딩 해제 방법입니다. 따라서 지금은 $(element).off("mouseenter mouseleave");.
Dennis98

10

바인딩 해제 mouseentermouseleave요소 (들)에 이벤트가 개별적으로 또는 바인딩 해제 모든 이벤트.

$(this).unbind('mouseenter').unbind('mouseleave');

또는

$(this).unbind();  // assuming you have no other handlers you want to keep

4

unbind ()는 하드 코딩 된 인라인 이벤트에서 작동하지 않습니다.

예를 들어에서 mouseover 이벤트를 바인딩 해제하려는 경우 이를 달성하는 빠르고 더러운 방법 <div id="some_div" onmouseover="do_something();">이라는 것을 알았 $('#some_div').attr('onmouseover','')습니다.


4

또 다른 해결책은 .die ()를 부착하는 것이 이벤트를 .live () .

전의.:

// attach click event for <a> tags
$('a').live('click', function(){});

// deattach click event from <a> tags
$('a').die('click');

여기에서 좋은 참고 문헌을 찾을 수 있습니다 : jQuery .live () 및 .die () 탐색

(내 영어 죄송합니다 : ">)


2

모든 hover는이면에서 수행되는 작업은 mouseover 및 mouseout 속성에 바인딩됩니다. 해당 이벤트에서 개별적으로 기능을 바인딩 및 바인딩 해제합니다.

예를 들어 다음 html이 있다고 가정합니다.

<a href="#" class="myLink">Link</a>

그러면 jQuery는 다음과 같습니다.

$(document).ready(function() {

  function mouseOver()
  {
    $(this).css('color', 'red');
  }
  function mouseOut()
  {
    $(this).css('color', 'blue');
  }

  // either of these might work
  $('.myLink').hover(mouseOver, mouseOut); 
  $('.myLink').mouseover(mouseOver).mouseout(mouseOut); 
  // otherwise use this
  $('.myLink').bind('mouseover', mouseOver).bind('mouseout', mouseOut);


  // then to unbind
  $('.myLink').click(function(e) {
    e.preventDefault();
    $('.myLink').unbind('mouseover', mouseOver).unbind('mouseout', mouseOut);
  });

});

수정, jquery src hover를 보면 실제로 mouseenter / mouseleave에 바인딩됩니다. 당신도 똑같이해야합니다.
bendewey

2

다음을 on사용하여 에 의해 첨부 된 특정 이벤트 핸들러를 제거 할 수 있습니다.off

$("#ID").on ("eventName", additionalCss, handlerFunction);

// to remove the specific handler
$("#ID").off ("eventName", additionalCss, handlerFunction);

이를 사용하면 handlerFunction 만 제거됩니다.
또 다른 좋은 방법은 여러 연결된 이벤트에 대한 네임 스페이스를 설정하는 것입니다.

$("#ID").on ("eventName1.nameSpace", additionalCss, handlerFunction1);
$("#ID").on ("eventName2.nameSpace", additionalCss, handlerFunction2);
// ...
$("#ID").on ("eventNameN.nameSpace", additionalCss, handlerFunctionN);

// and to remove handlerFunction from 1 to N, just use this
$("#ID").off(".nameSpace");

0

나는 이것이 .hover ()에 대한 두 번째 인수 (함수)로 작동한다는 것을 알았습니다.

$('#yourId').hover(
    function(){
        // Your code goes here
    },
    function(){
        $(this).unbind()
    }
});

첫 번째 함수 (.hover ()에 대한 인수)는 mouseover이며 코드를 실행합니다. 두 번째 인수는 #yourId에서 hover 이벤트의 바인딩을 해제하는 mouseout입니다. 코드는 한 번만 실행됩니다.


1
$.unbind()이것만으로 그 객체에서 모든 이벤트를 제거 하지 않습니까? 어떤 경우에 $.click()이벤트 와 같은 것이 이제 실패 할 것입니다.
Alexis Wilke 2014 년
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.