我正在升级的 Web 应用程序使用 jQuery 和 jQuery UI。我已经替换了大部分 window.open
的实例和 <a target=_blank>
使用 jQuery UI 对话框。例如,用于在新窗口中打开的条款和条件;现在我使用带有 AJAX 的 jQuery UI 对话框。为了保持一致性,我计划尽可能使用它。
一个这样的地方是一个页面,我将在其中提供指向视频的外部链接。像这样的东西:
<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 对话框来实现这一点?会不会有陷阱?
最佳答案
问题是:
- iframe 内容来自另一个域
- 需要为每个视频调整 iframe 尺寸
基于omerkirk's answer的解决方案涉及:
- 创建 iframe 元素
- 使用
autoOpen: false, width: "auto", height: "auto"
创建对话框 - 在打开对话框之前指定 iframe 源、宽度和高度
这里是一个粗略的代码大纲:
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");
});
});
关于jquery - 如何在 jQuery UI 对话框中显示 IFRAME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5660263/