将多个事件绑定到同一个函数 [英] Bind multiple events to same function

查看:87
本文介绍了将多个事件绑定到同一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将多个事件绑定到同一个函数。我想点击和触摸开始做同样的事情。然后我想要一个鼠标悬停的第二个功能。我想使用这种设置:

How can I bind mutiple events to the same function. I want click and touchstart to do the same. And then I want a second function for mouseover. I would like to use this kind of setup:

    $(document).on({
        click: function (event) {
        // I want this: click, touchstart: function (event) {
            event.preventDefault();
            // do something         
        },
        mouseover: function (event) {
            event.preventDefault();
            // do something         
        }
    }, '.link');


推荐答案

您可以将事件组合在一个<$ c $中c> .on()带空格:

You can combine the events in one .on() with space:

$(document).on('click touchstart', function(e) {
    // this will be called on click as well as touchstart
});

这篇关于将多个事件绑定到同一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆