javascript - Iframe 内容未加载到 JQuery 对话框中

标签 javascript jquery jquery-ui iframe jquery-ui-dialog

我有一个页面,我将在其中动态创建一个 iframe 并将其附加到 div 中。 该 div 将附加到 jquery 对话框。

<div id="diviframe"></div>
<script>
var iFrame = $('<iframe id="thepage" width="450" height="400"></iframe>').appendTo("#diviframe");
    var iFrameDoc = iFrame[0].contentDocument || iFrame[0].contentWindow.document;
    iFrameDoc.write('<p>Some useful html</p>');
    iFrameDoc.write('<p>Some useful stuff</p>');   
    iFrameDoc.close();


    $("#diviframe").dialog({

        autoOpen: false,
        modal: true,
        height: 500,
        width:500,
        open: function(ev, ui) {

    }

    });

$("#diviframe").dialog('open');

在 jquery 对话框中打开 iframe 时,不会写入 iframe 的内容。 谁能建议一个解决方法?

更新:

 function test() {
        var iFrame = $('<iframe id="thepage" width="500" height="500" src="http://stackoverflow.com"></iframe>').appendTo("#diviframe");
        var parent = "divparent";
        var iFrameDoc = iFrame[0].contentDocument || iFrame[0].contentWindow.document;
        iFrameDoc.write('<p>Some useful html</p>');
        iFrameDoc.write('<p>Some useful stuff</p>');
        iFrameDoc.close();      
        return iFrame;
    }






    var $dialog; //Must be at the global scope
    function dialog() {      
        alert(1);
        $.get(test, {}, function(html) {
            $dialog.html(html);
            $dialog.dialog("open");
        });
    }

    $(function() {
        //Initialize (without showing it)
        var $dialog = $('<div id="dialog" title=""></div>').dialog({
            autoOpen: false,
            modal: true
        });
    });
    dialog();

我尝试在 jquery 对话框加载后动态调用函数,但它显示一些错误。如何从 jquery 对话框加载函数?

最佳答案

我认为您不需要使用 iframe 来完成您正在做的事情,除非您在发布问题之前删除了一些重要的细节。

您可以将内容直接附加到#diviframe,它会正常显示。

$("#diviframe").append("<p>Some useful html</p>");
$("#diviframe").append("<p>Some useful stuff</p>");

我想知道 - 您是否正在尝试在对话框内创建一个可滚动面板?如果是这样,你可以设置一个div的CSS来实现你所需要的:

.myScrollableBox {
    width: 450px;
    height: 400px;
    overflow: auto;
    border: 1px solid #000;
}

您可能还想添加一些填充,这样文本就不会紧贴边框。

关于javascript - Iframe 内容未加载到 JQuery 对话框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22739013/

相关文章:

javascript - jQuery tablesorter 插件列宽在 IE7 中不正确

JavaScript 起点函数不稳定

jquery - 如何在打开事件之前隐藏 jquery 对话框?

jQuery UI Datepicker 仅启用数组中的特定日期

javascript - 上传多个文件夹

javascript - 如何将 JS 变量的值路由到 HTML Div?

javascript - 如何从页面中动态删除所有不需要的 CSS 和 JS

javascript - 允许星号的电子邮件的 JS 正则表达式

jquery - 在 HTML 中使 div 从底部 float

javascript - jquery自动完成动态插入的具有未知id的元素