如何在 jQuery UI 对话框中显示 IFRAME

2023-12-30

我正在升级的 Web 应用程序使用 jQuery 和 jQuery UI。我已经替换了大多数实例window.open and <a target=_blank>与 jQuery UI 对话框。例如,用于在新窗口中打开的条款和条件;现在我将 jQuery UI 对话框与 AJAX 结合使用。为了保持一致性,我计划尽可能使用它。

其中一个地方是一个页面,其中有视频的外部链接。就像是:

<a href="http://website.com/videos/1.html" target="_blank"><img src="http://website.com/videos/1.png"></a>
<a href="http://website.com/videos/2.html" target="_blank"><img src="http://website.com/videos/2.png"></a>

在某些情况下我可能会使用target=iframe1。现在,我不想在 iframe 或弹出窗口中打开内容,而是想在弹出对话框中显示内容。如何使用 jQuery UI 对话框来实现此目的?会有什么陷阱吗?


问题是:

  1. iframe 内容来自另一个域
  2. 需要针对每个视频调整 iframe 尺寸

解决方案基于奥莫柯克的回答 https://stackoverflow.com/a/5660409/87015涉及:

  • 创建 iframe 元素
  • 创建一个对话框autoOpen: false, width: "auto", height: "auto"
  • 指定 iframe 源、宽度和高度before打开对话框

这是代码的粗略轮廓:

HTML

<div class="thumb">
    <a href="http://jsfiddle.net/yBNVr/show/"   data-title="Std 4:3 ratio video" data-width="512" data-height="384"><img src="http://dummyimage.com/120x90/000/f00&text=Std+4-3+ratio+video" /></a></li>
    <a href="http://jsfiddle.net/yBNVr/1/show/" data-title="HD 16:9 ratio video" data-width="512" data-height="288"><img src="http://dummyimage.com/120x90/000/f00&text=HD+16-9+ratio+video" /></a></li>
</div>

jQuery

$(function () {
    var iframe = $('<iframe frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>');
    var dialog = $("<div></div>").append(iframe).appendTo("body").dialog({
        autoOpen: false,
        modal: true,
        resizable: false,
        width: "auto",
        height: "auto",
        close: function () {
            iframe.attr("src", "");
        }
    });
    $(".thumb a").on("click", function (e) {
        e.preventDefault();
        var src = $(this).attr("href");
        var title = $(this).attr("data-title");
        var width = $(this).attr("data-width");
        var height = $(this).attr("data-height");
        iframe.attr({
            width: +width,
            height: +height,
            src: src
        });
        dialog.dialog("option", "title", title).dialog("open");
    });
});

演示在这里 http://jsfiddle.net/H7uar/1/show/ and 代码在这里 http://jsfiddle.net/H7uar/1/. And 另一个类似的例子 http://salman-w.blogspot.com/2013/05/jquery-ui-dialog-examples.html#example-6

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 jQuery UI 对话框中显示 IFRAME 的相关文章