我正在制作一个由 Backbone.js 驱动的应用程序,正在尝试不同的方法来检测元素上的点击document
,这就是我想出的:
initialize: function() {
$(document).on('click', this.deselect.bind(this));
},
deselect: function(e) {
if (e.target.tagName == "HTML") {
return this.collection.deselect();
}
}
它的工作原理<html>
是在单击时元素作为事件目标传递document
。但是jQuery 文档建议我应该比较this
(event.target
在我的情况下不起作用)或停止事件的传播。
最好的方法是什么?我也想知道为什么没有人像我那样做?
更新:
由于原始帖子我发现最好检查事件目标是否<html>
并更新代码。