2013-01-30 2 views
0

"mouseenter"및 "mouseleave"이벤트를 바인딩 해제하려는 요소를 클릭하면 제대로 작동하지만 다른 요소를 클릭하면 다시 바인드하고 싶습니다. -이 작동하지 않습니다.jQuery "bind"back to bind "no"

어떤 도움이 필요합니까?

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#shape1 img").click(function(){ 
      $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").unbind('mouseenter mouseleave'); 
    }); 

    $("#close").click(function(){ 
     $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").bind('mouseenter mouseleave'); 
    }); 
}); 
</script> 

많은 감사 :

여기에 코드입니다!

+0

바인드에 함수를 연결해야합니다. bind ('mouseenter mouseleave', function() { $ (this) .toggleClass ('entered'); }); –

+0

(a) 왜'bind' /'unbind'를 사용하고'on' /'off'하지 않습니까? (b) 이벤트 핸들러를 첨부하려면 하나를 지정해야합니다! –

답변

0

.bind() 함수는 이벤트가 트리거 될 때 실행할 함수를 전달해야합니다.

$("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").bind('mouseenter mouseleave', function(event) { 
    // do something here when the mouseenter or mouseleave events are triggered 
}); 

이벤트 핸들러가 완전히 제거되고, jQuery를 그것이 무엇인지 기억하지 않습니다 .unbind() 전화

. .bind()을 호출하여이를 취소하고 해당 이벤트에 대한 응답으로 어떤 코드가 실행되어야하는지 알 수 없습니다.

또한 jQuery (1.7 이상)의 버전에 따라 이벤트 처리기 추가 및 제거에 .on().off() 함수를 사용해야합니다.

+0

흠, 기본적으로 모든 모양이 다른 모양 (애니메이션, 효과 등)을 수행 할 때 사용됩니다. 이 애니메이션, 효과를 무능하게하고 싶습니다. 무언가를 클릭했을 때 정적이어서 움직일 수 없으므로 다른 요소를 클릭하면 다시 작동하게됩니다. – Marcel

+0

@Marcel 그런 경우, 그 함수 내에서 컨트롤 부울을 사용하여 애니메이션 등을 수행할지 여부를 결정하는 것이 더 나을 것입니다. 이미지 중 하나를 클릭 할 때 false로 설정 한 다음 '# close' 요소를 클릭하면 다시 사실이됩니다. –

+0

아니면 먼저 이러한 이벤트 처리기를 추가하는 코드가 있다고 가정합니다. 이를 함수로 옮기고 이벤트 핸들러를 리 바인드해야 할 때 간단히 호출 할 수 있습니다. –

0

이벤트가 발생할 때 실행할 콜백을 지정해야하기 때문에.

시도 :

<script type="text/javascript"> 
    $(document).ready(function(){ 
     var myFunctionMouseEnter = function(){ 
      alert('Hey'); 
     }; 
     var myFunctionMouseleave = function(){ 
      alert('Hey'); 
     }; 

     $("#shape1 img").click(function(){ 
      $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").off('mouseenter mouseleave'); 
     }); 

     $("#close").click(function(){ 
      $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").on('mouseenter',myFunctionMouseEnter) 
                       .on('mouseleave',myFunctionMouseleave); 
     }); 
}); 
</script> 
+0

나는 각 모양이 다른/독립적 인 것들을 마우스 입력 할 때 콜백을 추가하고 싶지 않다. – Marcel

+0

이벤트를 일시적으로 바인드 해제 할 수는 없습니다. 모든 경우에 사용할 콜백을 다시 지정해야합니다. – sdespont

+0

또 다른 한편으로, 글로벌 변수를 할당하여 콜백이 실행되지 않아야 하는지를 정의 할 수 있습니다. 콜백을 처음 체크인 할 때 다음과 같이됩니다. if (window.unboundEvents) false를 반환합니다. – sdespont

0

바인드는 현재 기존 항목에 대한 이벤트 처리기를 바인딩합니다. 문서로부터

Bind()

처리기는 jQuery를 개체에서 현재 선택된 요소에 연결되므로 그 소자 .bind()에 지점에서 호출 있어야는

발생 On 메소드를 사용하십시오.

$("#shape1 img").click(function(){ 
      $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").off('mouseenter'); 
     }); 

     $("#close").click(function(){ 
      $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").on('mouseenter',myFunction1); 
      $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").on('mouseleave',myFunction2); 
     }); 
+0

'.on()'의 사용법은'.bind()'의 현재 사용법과 기능적으로 동일합니다. 똑같은 일을 할 것입니다. 그 질문에 그가 동적으로 추가 된 요소로 작업한다는 질문은 없지만, 그렇다면'.on()'이 그 요소를 처리하는 올바른 구문이 아닙니다. –