微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用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();">
    test test
</div>

解决方法

你应该把一个id放在div上,说“test”,然后:
$('#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...
});

HTML将如下所示:

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

原文地址:https://www.jb51.cc/jquery/182006.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐