我有十个这样的代码块,都是动态创建的内容:
$(document).on('click', '.item' , function() {
//... do something
});
$(document).on('click', '.element' , function() {
//... do something different
});
有没有办法写得更短或更高效?
最佳答案
您可以缓存您的文档选择器。
var $document = $(document);
$document.on('click', '.item' , function() {
//... do something
});
$document.on('click', '.element' , function() {
//... do something different
});
但仅此而已。
编辑:为我的 jsPerf 贡献一个测试如果你想要硬数据。
关于javascript - 有没有办法写这个更短或更有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20440683/