使用jQuery将单个事件处理程序绑定到多个事件 [英] Binding a single event handler to multiple events with jQuery

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

问题描述

我有以下div与onblur,onmousedown,onmouseup和onfocus不同的功能。我想最小化的代码,只有一个函数调用内部的div所有的功能状态。我想用jquery做这个,所以换句话说。我想创建一个名为functionAll的函数,并将所有的function1都放在函数4中,其内部是鼠标状态,然后在Div中使用functionAll。我该怎么做这样的事情?

 < div contentEditable =true
onblur =function1()
onmousedown =return function2(event);
onmouseup =function3();
onfocus =function4();>
测试测试
< / div>


解决方案

你应该在div上放一个id,测试,然后:

  $('#test')bind('blur mousedown mouseup focus',function(e) {
//这是你的组合函数
//在这里你可以使用e.type来确定它是否是
//blur,mousedown,mouseup ,etc ...
});

HTML将如下所示:

 < div id =testcontentEditable =true> 
测试测试
< / div>


I have the following div with different functions for onblur, onmousedown, onmouseup, and onfocus.. I want to minimize the code and have only one function call inside div for all the function states. I want to do this with jquery So in other words. I want to create a function called functionAll and put all of function1 to function 4 with it's mouse state inside, then just use functionAll inside the Div. How can I do something like this?

<div contentEditable="true"
     onblur="function1();"
     onmousedown="return function2(event);"
     onmouseup="function3();"
     onfocus="function4();">
    test test
</div>

解决方案

You should put an id on the div, say "test", then:

$('#test').bind('blur mousedown mouseup focus', function (e) {
    // This is your combined function
    // Inside here you can use e.type to find out whether it was a
    // "blur", "mousedown", "mouseup", etc...
});

The HTML will look like:

<div id="test" contentEditable="true">
    test test
</div>

这篇关于使用jQuery将单个事件处理程序绑定到多个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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