组合悬停和点击函数(jQuery)? [英] Combine hover and click functions (jQuery)?
本文介绍了组合悬停和点击函数(jQuery)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以将悬停和点击功能合并为一个,例如:
Can hover and click functions be combined into one, so for example:
点击:
$('#target').click(function() {
// common operation
});
hover:
$('#target').hover(function () {
// common operation
});
它们可以组合成一个函数吗?
can they be combined into one function?
谢谢!
推荐答案
使用基本编程组合:并将相同的函数传递给单击
和 hover
作为回调。
Use basic programming composition: create a method and pass the same function to click
and hover
as a callback.
var hoverOrClick = function () {
// do something common
}
$('#target').click(hoverOrClick).hover(hoverOrClick);
第二种方法:使用 bind
在
上:
$('#target').on('click hover', function () {
// Do something for both
});
这篇关于组合悬停和点击函数(jQuery)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文