【问题标题】:jquery dialog .html() questionjquery 对话框 .html() 问题
【发布时间】:2011-10-04 03:32:05
【问题描述】:

我正在使用 jquery 对话框并希望使用位于同一服务器上的外部 html 文件设置 .html 值。我不确定究竟如何实现这一目标。

var $tos_dlg = $('<div></div>')
  .html($(this).load('/includes/tos.html'))
  .dialog({
    autoOpen: false,
    title: 'Policies &amp; Terms of Service',
    width: 600,
    height: 400,
    modal: true
});

上面调用 .html() 的部分是我要注入外部文件内容的地方。我认为 .load 函数会以某种方式工作,但只是不确定这是否是正确的方法,如果是,如何准确地实现它。有人可以帮忙吗?

谢谢

【问题讨论】:

    标签: jquery html dialog external


    【解决方案1】:

    直接拨打$tos_dlg.load()

    var $tos_dlg = $('<div></div>')
        .load('/includes/tos.html')
        .dialog({
            autoOpen: false,
            title: 'Policies &amp; Terms of Service',
            width: 600,
            height: 400,
            modal: true
        });
    

    另外,请确保您通过$tos_dlg.appendTo("#containerElement") 之类的方式将$tos_dlg 附加到DOM。

    【讨论】:

    • 优秀!那是完美的解决方案!非常感谢你,gilly3!
    • 请原谅我的问题,但它有什么作用或需要它? appendTo() 就是。
    • 使用 append 或 appendTo 将新内容插入页面。您使用$("&lt;div&gt;") 创建了一个新的
      元素。您可以随心所欲地使用该新元素,但在您将其放在那里之前,它不会出现在您的页面中。语法为existingContent.append(newContent)newContent.appendTo(existingContent)
    【解决方案2】:

    试试这个:

    var $tos_dlg = $('<div></div>').html($(this).load('/includes/tos.html'));
    $("body").append($tos_dlg);
    $tos_dlg.dialog({
        autoOpen: false,
        title: 'Policies &amp; Terms of Service',
        width: 600,
        height: 400,
        modal: true
    });
    

    【讨论】:

    • 我确实试过这个,但我要么做错了,要么没用。当我回到这里尝试检查代码时,gilly3 的答案就在这里,并且效果很好,但是感谢您的好建议。
    猜你喜欢
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多