2012-05-24 27 views
1

在同一個div上掛上懸停和onclik功能的正確方法是什麼?懸停並點擊同一div上的功能

這裏是我試過,但它沒有工作(點擊什麼都不做):

$(function outlineHider(){ 
    $("#hotspot").hover(
     function() { 
      $("#outlines").show(); //showing another div element 
      function bindClick() { 
       $("#hotspot").click(callAnotherFunction())}; 
      }, 
      function() { 
       $("#outlines").hide(); 
      } 
     ); 
}); 
+2

可能重複 - http://stackoverflow.com/questions/2432003/combine-hover-and-click-functions-jquery – Neil

回答

2

試試這個:

$(function() { // document ready handler shortcut 
    $("#hotspot").hover(
     function() { $("#outlines").show(); }, 
     function() { $("#outlines").hide(); } 
    ).click(function() { 
     callAnotherFunction() 
    }); 
}); 

或者更短的版本,感謝@VisioN:

$(function() { 
    $("#hotspot") 
     .hover(function() { $("#outlines").toggle(); }) 
     .click(callAnotherFunction);  
}); 
+1

'$( 「#熱點」)懸停(函數(){$(」 #outlines「)。toggle();})'也可以工作,但更短。 – VisioN

+0

非常棒,非常感謝! –

+0

沒問題,很樂意幫忙。 –

0

試試這個

$(function() { 
    $("#hotspot") 
     .hover(
      function() { $("#outlines").show(); }, //mousein 
      function() { $("#outlines").hide(); } //mouseout 
     ) 

     .click(function() { 
      callAnotherFunction(); 
     }); 
}); 
0

如果我說得對,這應該對你有幫助。

$(function outlineHider(){ 
    $("#hotspot").hover(
     function() { 
      $("#outlines").show(); //showing another div element 
    }),click(function(){ 
     $("#outlines").hide(); 
    }); 
}); 

Combine hover and click functions (jQuery)?

VAR hoverOrClick =函數(){// 做共同之處} $( '#目標')點擊(hoverOrClick).hover(hoverOrClick)。

方式二:使用綁定:

$( '#目標')綁定( '點擊懸停',函數(){// 做兩個東西});