给iframe 添加onclick事件

2017-11-08  本文已影响0人  繁华荆棘一路向北

首先 ,建立一个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'); });

上一篇 下一篇

猜你喜欢

热点阅读