gpt4 book ai didi

jquery - 使用 jQuery 将单个事件处理程序绑定(bind)到多个事件

转载 作者:行者123 更新时间:2023-12-03 21:49:38 25 4
gpt4 key购买 nike

我有以下 div,具有 onblur、onmousedown、onmouseup 和 onfocus 的不同功能。我想最小化代码,并且在 div 内只有一个函数调用来处理所有函数状态。我想用 jquery 来做到这一点 所以换句话说。我想创建一个名为 functionAll 的函数,并将所有 function1 放入 function 4 中,其中包含鼠标状态,然后只需在 Div 中使用 functionAll 即可。我怎样才能做这样的事情?

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

最佳答案

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

$('#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>

关于jquery - 使用 jQuery 将单个事件处理程序绑定(bind)到多个事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4710219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com