我在页面上有一个 Iframe 元素,我需要为 iframe 内的对象创建一些原型(prototype)方法。
我有 iframe 窗口
var win = iframe.contentWindow || iframe.contentDocument.defaultView;
然后尝试创建方法。在 FF、Chrome 和 IE9+ 中,这很简单:
win.Object.prototype.func = myFunc;
但它在 IE8 中不起作用。我在 win.Object 中未定义。 同时win.Element工作完美。
如何在 IE8 中获取 iframe 对象?
最佳答案
您可以在 iframe
中插入一个 script
元素,该元素在外部窗口中创建一个指向内部对象
的全局变量:
var doc = iframe.contentDocument,
s = doc.createElement('script');
s.text = "parent.Object2 = Object;";
doc.appendChild(s);
doc.removeChild(s);
然后,您可以使用Object2.prototype
。
关于javascript - IE8中访问iframe Object.prototype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31547091/