javascript - 如何在 jQuery 中拆分多个事件句柄?
问题描述
试图弄清楚如何在新行中拆分多个事件以使下面的代码更清晰。
$document.find('body').on(
'click dblclick mousedown mousemove mouseout mouseover mouseup mousewheel keydown keypress keyup textInput touchcancel touchend touchmove touchstart blur change focus reset resize select scroll submit zoom',
testConsole
);
function testConsole() {
console.log(' test');
}
我怎样才能使这段代码更干净?现在我有一个包含我想要处理的所有事件的单行,是否可以将它放在一个数组或其他东西中以使其可读?
解决方案
array
您可以使用,然后join
使用创建事件space
。检查下面的代码。
var events = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'mousewheel', 'keydown', 'keypress', 'keyup', 'textInput', 'touchcancel', 'touchend', 'touchmove', 'touchstart', 'blur', 'change', 'focus', 'reset', 'resize', 'select', 'scroll', 'submit', 'zoom'];
events = events.join(" ");
$(document).find('body').on(events,testConsole);
function testConsole() {
console.log(' test');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>dsdsdsd</body>
推荐阅读
- typescript - 在 TypeScript 中,是否可以声明一个属性名称都属于某个枚举的对象?
- php - 在第 4 行的 C:\Users\Logan\Cart\bootstrap\app.php 中的 bootstrap\app.php:4 #1 {main} 中找不到类 'app'
- r - 从深度嵌套的子目录中读取文件,并用 R 中的子目录全路径名重命名它们
- c - 仅使用套接字实现单进程管道
- dsl - Xtext DSL 编辑器引用了我的模型中定义的 Java 类
- python - 确定文本语言和纠正 Python 中的拼写错误的最佳算法是什么?
- c# - 我怎样才能扔掉捡到的物体?
- react-native - 如何使用 useFocusEffect 挂钩
- google-my-business-api - 无法获取任何帐户信息或评论信息
- spatial - 如何在适合逻辑回归正交方案的泊松品脱过程模型中包含调查权重?