创建一个 iframe,然后使用 jQuery 向其附加数据
我正在尝试对greasemonkey 用户脚本进行一些修改以实现我需要的功能。代码就像
showAddress:function(addrString,type)
{
this.addrBox=$('<div id="batchPublish"></div>')
.append('<div id="batchHeader"></div>')
.append('<div id="batchContent" style="float:left;clear:both"></div>');
.........
var batchContent=this.addrBox.find('#batchContent')
.append('<pre width="300" style="text-align:left" id="batchedlink"></pre>');
this.addrBox.find('#batchedlink').css({'width':'500px','height':'250px','overflow':'auto','word-wrap': 'break-word'})
.append(addrString);
$.blockUI({message:this.addrBox,css:{width:"520px",height:"300px"}}); }
基本上这段代码将数据写入html。我想要实现的是将“addrString”写入嵌入的 iframe 中。现在它位于“pre”标签中。我尝试了很多方法但仍然没有运气。 iframe 总是空的。 我完全是 javascript 的新手,不清楚这是否可行。
谢谢你的帮助。
I am trying do some modification to an greasemonkey userscript to implement a feature I need. The code is like
showAddress:function(addrString,type)
{
this.addrBox=$('<div id="batchPublish"></div>')
.append('<div id="batchHeader"></div>')
.append('<div id="batchContent" style="float:left;clear:both"></div>');
.........
var batchContent=this.addrBox.find('#batchContent')
.append('<pre width="300" style="text-align:left" id="batchedlink"></pre>');
this.addrBox.find('#batchedlink').css({'width':'500px','height':'250px','overflow':'auto','word-wrap': 'break-word'})
.append(addrString);
$.blockUI({message:this.addrBox,css:{width:"520px",height:"300px"}}); }
Basically this code writes data to html. What I want to implement is to have "addrString" written to an iframe embedded. Now It's in the "pre" tag. I have tried many approaches but still no luck. Iframe was always empty.
I am completely a novice in javascript and unclear whether this is possible.
Thank you for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您要在同一域中添加 iFrame,因此您可以像这样操作其内容:
(在 jsBin 上查看它的实际效果。)
注意:
对于 Chrome 用户脚本,您显然不需要计时器延迟。但对于 FF 和 IE 8(我仔细检查过的其他 2 个浏览器),动态添加的 iFrame 在由于某种原因“稳定”之前是不可操作的。这似乎大约需要 200 毫秒。
静态加载的 iFrame 没有这种延迟,请参阅 jsBin 演示。
Since you are adding the iFrame in the same domain, then you can manipulate its contents like this:
(See it in action at jsBin.)
NOTE:
For a Chrome userscript, you apparently don't need the timer delay. But for FF and IE 8 (the other 2 browsers I double-checked), a dynamically added iFrame is not manipulable until after it has "settled" for some reason. This seems to take about 200 mS.
A statically loaded iFrame does not have this lag, see the jsBin demo.
有点难以准确说出您在问什么 - 但如果您想知道是否可以将 DOM 元素附加到 iFrame,答案是“不”。
Sort of hard to tell exactly what you're asking -- but if you want to know whether or not you can append DOM elements to an iFrame, the answer is "no".