function ClickAway (obj) {
this.el = $(obj.id);
this.clickCb = obj.callback;
this.init();
}
ClickAway.prototype = {
constructor: ClickAway,
init: function () {
this.bindClickAway();
},
checkClickAway: function (e) {
var target = e.target;
if (this.el[0] != target &&
document.documentElement.contains(target) &&
!this.isDescendant(this.el, target)) {
this.clickCb();
}
},
bindClickAway: function () {
$(document).on('touchstart', $.proxy(this.checkClickAway, this));
},
isDescendant: function (parent, child) { //判断是否为后裔元素,如果是则不执行父元素上的方法,既阻止冒泡
var node = child.parentNode;
while (node != null) {
if (node == parent) return true;
node = node.parentNode;
}
return false;
}
};