创建一个 iframe,然后使用 jQuery 向其附加数据

发布于 2024-12-20 05:07:16 字数 892 浏览 0 评论 0原文

我正在尝试对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的他你的她 2024-12-27 05:07:16

由于您要在同一域中添加 iFrame,因此您可以像这样操作其内容:
(在 jsBin 上查看它的实际效果。)

$("#batchContent").append ('<iframe id="batchedlink"></iframe>');

/*--- Compensate for a bug in IE and FF, Dynamically added iFrame needs 
    some time to become "DOM-able".
*/
setTimeout ( function () {
        var iframeBody  = $("#batchedlink").contents ().find ("body");

        iframeBody.append (addrString);
    },
    333
);

注意:
对于 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.)

$("#batchContent").append ('<iframe id="batchedlink"></iframe>');

/*--- Compensate for a bug in IE and FF, Dynamically added iFrame needs 
    some time to become "DOM-able".
*/
setTimeout ( function () {
        var iframeBody  = $("#batchedlink").contents ().find ("body");

        iframeBody.append (addrString);
    },
    333
);

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.

望笑 2024-12-27 05:07:16

有点难以准确说出您在问什么 - 但如果您想知道是否可以将 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文