2012-12-13 2 views
0

div 요소가 있고 마우스를 입력 할 때 함수를 실행하고 싶습니다. 하지만 실제로는 페이지의 중앙에 있으므로 마우스가 일반적으로 페이지로드 및 기능 실행에서 실행됩니다. 마우스가 이미 있지만 입력 할 때 내 기능이 실행되지 않도록 할 수있는 방법이 있습니까?마우스가 이벤트를 입력하지만 마우스가 아직 입력되어 있지 않은 경우에만

<div id = "mouse-control-div">.....</div> 
<script type="text/javascript"> 
    $('#mouse-control-div').mouseover(function() {alert('some thing');}); 
</script> 

나는 또한 .mouseenter를 시도했지만 동일한 결과가 나타납니다.

(코드 샘플이 추가됩니다)

답변

0

당신이 이미 입력 한 경우를 알려주는 부울이 있다면? 에 MouseOver /로 마우스를 MouseEnter /하는 MouseLeave look here

// When the DOM is ready, init scripts. 
jQuery(function($){ 
    var isEntered = false; 
    // Bind the mouse over /out to the first DIV. 
    $("#mouse-control-div").bind(
     "mouseover mouseout", 
     function(event){ 
      if(!isEntered) 
      { 
       isEntered = true; 
       echo('entered'); 
      } 
     } 
    ); 

    // Bind the mouse enter to the second DIV. 
    $("#mouse-control-div").bind(
     "mouseenter mouseleave", 
     function(event){ 
      if(isEntered) 
      { 
       isEntered = false; 
       echo('left'); 
      } 
     } 
    ); 

    function echo(string) 
    { 
     $('#output').append('<li>'+string+'</li>'); 
    } 

}); 

대 작업 데모 look here

+0

감사의 경우 : jQuery를 이벤트에 대한 자세한 내용은

! 내가 '_echo ('입력 ')이라고 말하면 _ 내가 원하는대로 작동했습니다. –

+0

jsfiddle을 보면 결과를 보여주기 위해'echo()'함수를 추가했다. –

관련 문제