为 iframe 扩展文档 DOM
我已经有一组用于扩展文档的函数(即绑定/取消绑定)。所以我可以做类似的事情: document.bind('load',someAction,{})
但如果我这样做,它就不起作用: $('some_iframe').contentWindow.document .bind(...)
显然, $('some_iframe').contentWindow.document.prototype
不存在。
编辑: 这是代码细分:
//the eggplant library
eggp = {
extend: function(dest, source){
for(var prop in source)
dest.prototype[prop] = source[prop];
return dest;
},
//other functions below...
bind{},
unbind{}
}
//extend the DOM
eggp.extend(Document, eggp);
//extending the iframe document DOESN'T WORK
eggp.extend(someiframe.contentWindow.document, eggp);
我检查了 someiframe.contentWindow.document
是否未定义,但它返回 object HTMLDocument
I already have a set of functions I use to extend the document (i.e. bind/unbind). So I can do stuff like: document.bind('load',someAction,{})
But it doesn't work if I do: $('some_iframe').contentWindow.document.bind(...)
And apparently, $('some_iframe').contentWindow.document.prototype
doesn't exist.
EDIT:
Here is the code breakdown:
//the eggplant library
eggp = {
extend: function(dest, source){
for(var prop in source)
dest.prototype[prop] = source[prop];
return dest;
},
//other functions below...
bind{},
unbind{}
}
//extend the DOM
eggp.extend(Document, eggp);
//extending the iframe document DOESN'T WORK
eggp.extend(someiframe.contentWindow.document, eggp);
I've checked to see if someiframe.contentWindow.document
is undefined, but it returns object HTMLDocument
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能这样做,至少不能以跨浏览器的方式。理想情况下,您应该扩展
HTMLDocument
对象的prototype
,但当然,IE 首先并没有这个对象。作为解决方法,您可以创建一个函数来创建 IFrame 并自动扩展其document
对象,并专门使用该函数来创建 IFrame,但如果框架已经在页面上,则不会出现“除了循环遍历每个并手动扩展它之外,您无能为力。(假设使用 jQuery,在此处插入您自己的库代码以获取所有 IFrame。)
不过,一般来说,扩展内置 DOM 对象是坏主意,所以你应该想出另一种方法来做你想做的事。
You can't do this, at least not in a cross-browser way. Ideally you would extend the
prototype
of theHTMLDocument
object, but of course, IE does not have this object in the first place. As a workaround, you could create a function that creates an IFrame and automatically extends itsdocument
object, and exclusively use that function to create your IFrames, but if the frames are already on the page, there isn't much you can do about it short of looping through each one and extending it manually.(Assuming jQuery, insert your own library code to get all IFrames here.)
In general, though, extending built-in DOM objects is a bad idea, so you should come up with another way to do what you want.
您最初是如何扩展
document
的?您是否必须扩展HTMLDocument.prototype
而不是document.prototype
,因为后者不是构造函数?这意味着您在 iframe 中扩展了错误的内容?如果我错了,请告诉我你最初是如何扩展它的。
How are you extending
document
initially? Don't you have to extendHTMLDocument.prototype
instead ofdocument.prototype
, since the latter isn't a constructor?Meaning you're extending the wrong thing in the iframe? If I'm wrong, please tell me how you are extending it initially.
我猜“some_iframe”是一个ID。要使用 jQuery 通过 ID 选择对象,您需要使用
#
,就像在 CSS 中一样。所以它应该是
$('#some_iframe').eq(0).contentWindow.document.prototype
。如果您不使用
#
,则用于按标签名称进行选择。因此
$('p')
将选择页面上的所有标记。
I am guessing "some_iframe" is an ID. To select an object by ID with jQuery you need to use
#
, just like in CSS.So it should be
$('#some_iframe').eq(0).contentWindow.document.prototype
.If you don't use
#
that is used to select by TAG name.So
$('p')
would select all<p>
tags on the page.