Jquery结合点击和更改一次执行 [英] Jquery combine click and change for one execution

查看:79
本文介绍了Jquery结合点击和更改一次执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

将功能一次性绑定到不同元素的多个事件

我需要为链接点击和更改事件执行相同的代码

I need to execute the same code for both a link click and change event

$(".foo").click(function() {

  //same code

}

$(".bar").change(function() {

  //same code

}

我要实现的正常方法是创建一个函数,然后在每个单独的事件上触发函数 - 但我想知道jquery是否有更简单的方法来处理它,类似于

The normal way I would achieve is to create a function and then fire the function on each seperate event - but I'm wondering if jquery has an easier way to handle this, similar to

 $(".bar, .foo").

但很明显有不同的事件方法。

But obviously with different event methods.

为了澄清我需要一个点击而一个要更改但不要同时点击和更改, THA有意义吗?

To clarify i need one to click and one to change but NOT both to click and change, does that make sense?

推荐答案

$(".foo, .bar").on("click change", function(e){

    var myClass = $(this).is(".foo") ? "foo" : "bar";

    if(e.type === "change"){
         //DO STUFF UNIQUE TO CHANGE
    }
    else if(e.type === "click") {
         //DO STUFF UNIQUE TO CLICK
    }

    //DO STUFF THAT IS THE SAME

});

这篇关于Jquery结合点击和更改一次执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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