监听嵌套iframe页面操作事件

监听嵌套iframe页面操作事件

在页面中插入一个空input:

js方法:

var IframeOnClick = {
        resolution: 200,
        iframes: [],
        interval: null,
        Iframe: function () {
            this.element = arguments[0];
            this.cb = arguments[1];
        },
        track: function (element, cb) {
            this.iframes.push(new this.Iframe(element, cb));
            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) {
                        this.iframes[i].cb.apply(window, []);
                        var btn = document.getElementById("btn");
                        btn.focus();
                    }
                }
            }
        }
    };
     IframeOnClick.track(document.getElementById("iFrame"), function () {
     console.log("iframe操作事件触发了");
         yourFunction();
     })

你可能感兴趣的:(技术备份)