0

我们可以在事件类型上设置条件,例如如果事件click然后警报“点击”如果事件mouseover然后警报“结束”。但是当功能在页面上加载时,我的功能会发出警报值

<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>
4

6 回答 6

3

这种情况下的想法是为不同的事件类型定义不同的处理程序:

   $('.wait').click(function(){
        alert('click')
    });
   $('.wait').mouseenter(function(){
        alert('mouseenter')
    });
于 2012-10-01T15:19:29.147 回答
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)
});
于 2012-10-01T15:20:27.973 回答
2

试试这个

(document).ready(function() {
   $('.wait').bind('click dblclick mousedown mouseenter mouseleave',
               function(e){
               alert('Current Event is: ' + e.type);
                    });
                   });
于 2012-10-01T15:23:29.067 回答
0

简单地尝试

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

   $('.wait').mouseenter(function(){
        alert('mouseenter')
    }); 
于 2012-10-01T15:20:36.097 回答
0

编写单独的事件来处理它们..

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

   $('.wait').on('mouseenter'f,unction(){
        alert('MouseEnter Event !!')
    });
于 2012-10-01T15:21:07.483 回答
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);
});
于 2012-10-01T15:28:25.200 回答