2012-10-01 40 views
0

我們可以設置條件的事件類型,如事件是click然後提醒'點擊'如果事件如果mouseover然後提醒'超過'。但我的功能是提醒值,當功能被加載頁面上jquery中的事件類型條件

<head> 
<script type="text/javascript" src="jquery-1.7.2.js"></script> 

<script type="text/javascript"> 
$(function() { 

    if($('.wait').click()) { 
     alert('click') 
    } 
    else if ($('.wait').mouseenter()) { 
     alert('mouseenter') 
    } 
}) 
</script> 

<style> 
    .wait {color:#F00} 
    .nowait {color:#00F} 
</head> 

<body> 
    <div class="wait">abc.....</div> 
    <div class="wait">abc.....</div> 
    <div class="wait">abc.....</div> 

</body> 

回答

2

試試這個

(document).ready(function() { 
    $('.wait').bind('click dblclick mousedown mouseenter mouseleave', 
       function(e){ 
       alert('Current Event is: ' + e.type); 
        }); 
        }); 
+0

這是捕捉你想要的任何事件並提醒它 – fatiDev

3

在這種情況下,我們的想法是定義不同的處理程序,不同的事件類型:

$('.wait').click(function(){ 
     alert('click') 
    }); 
    $('.wait').mouseenter(function(){ 
     alert('mouseenter') 
    }); 
3

你的語法錯誤,請改用:

$(".wait") 
.click(function(event) { 
    alert("click"); 
    // do want you want with event (or without) 
}) 
.mouseenter(function(event) { 
    alert("mouseenter"); 
    // do want you want with event (or without) 
}); 
+1

+1。 –

0

儘量簡單

$('.wait').click(function(){ 
     alert('click') 
    }); 

$('.wait').mouseenter(function(){ 
     alert('mouseenter') 
    }); 
0

寫了獨立的事件來處理他們..

$('.wait').on('click',function(){ 
     alert('Click Event !!'); 
    }); 

    $('.wait').on('mouseenter'f,unction(){ 
     alert('MouseEnter Event !!') 
    }); 
0

如果要綁定多個事件處理程序,以相同的對象,我會親自將事件圖(對象)傳遞給.on()函數,如下所示:

$('.wait').on({ 
    click: function(e) { 
     alert('click'); 
     // handle click 
    }, 
    mouseover: function(e) { 
     alert('mouseover'); 
     // handle mouseover 
    } 
}); 

然而,如果所有你想要做的是輸出的事件類型,有做一個簡單的方法:對於是唯一一個至今演示鏈接

$('.wait').on('click mouseover', function(e) { 
    alert(e.type); 
});