监听iframe中的鼠标点击事件

<iframe id="iframe1" frameborder="0" name="iframe1" scrolling="no"
        src="https://www.baidu.com">
    <p>你的浏览器不支持iframes.</p>
</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(new this.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 i in this.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;  
               }  
           }  
       }  
   }  
}; 
复制代码
复制代码
// 调用
/*
var iframe1 = document.getElementById('iframe1');
IframeOnClick.track(iframe1, function() { 
    alert('iframe被点击了!')
});
*/

/* jQuery测试
 * 不要在父窗口监控,应该是在Iframe窗体里面,执行了某个事件之后,触发父窗口页面的方法!
*/
$('body #iframe1').load(function(){
    $(this).contents().find('#PG01:j_id_2q_2_9_6_f_4_3_5').on('click', function () {
        alert('确定进行下载操作?');
    })
})
复制代码

 

posted @   夜独醉  阅读(700)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示