首先 ,建立一个iframe对象,形成原型
var IframeOnClick = {
resolution: 200,
iframes: [ ],
interval:null,
Iframe:function() {
this.element = arguments[0];
this.cb = arguments[1];
this.hasTracked =false;
},
track:function(element, cb) {
this.iframes.push(newthis.Iframe(element, cb));
if (!this.interval) {
var _this =this;
this.interval = setInterval(function() { _this.checkClick(); },this.resolution);
}
},
checkClick:function() {
if (document.activeElement) {
var activeElement = document.activeElement;
for (var iinthis.iframes) {
if (activeElement ===this.iframes[i].element) {// user is in this Iframe
if (this.iframes[i].hasTracked ==false) {
this.iframes[i].cb.apply(window, []);
this.iframes[i].hasTracked =true;
}
}else {
this.iframes[i].hasTracked =false;
}
}
}
}
};
最后直接调用
IframeOnClick.track(document.getElementById("iFrame"),function() { alert('a click'); });