我已经创建了诸如 hasClass()
之类的原型(prototype),因此我可以调用 e.target.hasClass('className');
。现在,当我调用原型(prototype)时它可以工作,但是我的页面上有一个 iframe,当我在页面构建器上工作时,我一直进出该 iframe。但是,如果我引用 iframe 中的元素,则该元素不存在原型(prototype)函数。
如何让 Element.prototype
函数在父级和 iframe 中工作?
最佳答案
您需要访问加载事件处理程序内的 iframe 窗口,并且可以执行如下操作:
var frame = document.getElementById('ifrm');
frame.onload = function() {
var win = this.contentWindow || this.contentDocument;
win.Element.prototype.test = function() {
this.innerHTML = 'Iframe Prototype content'
}
win.document.getElementById('heading').test()
}
因此,如果您要将所有新原型(prototype)属性包装到一个函数中,您可以在主窗口上下文和框架窗口上下文中调用该函数
关于javascript - 如何允许iframe访问Element.prototype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40694268/