如何在对话框JQUERY UI中打开URL

Dan*_*aad 7 html jquery jquery-ui jquery-ui-dialog

我一直在寻找一个简单的解决方案.我希望在JQuery UI对话框窗口中显示一个页面(例如http://www.google.com).计划是稍后动态添加URL,以便我站点中的所有链接都显示在所述窗口中.

我尝试了以下操作,但单击链接时对话框窗口为空.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test</title>


<meta charset="utf-8" />
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
  <script>
$(document).ready(function() {
    $('#openwindow').each(function() {
        var $link = $(this);
        var $dialog = $('<div></div>')
            .load($link.attr('href'))
            .dialog({
                autoOpen: false,
                title: $link.attr('title'),
                width: 500,
                height: 300
            });

        $link.click(function() {
            $dialog.dialog('open');

            return false;
        });
    });
});
  </script>

</head>
<body>
<a id="openwindow" href="http://www.google.com">Click me to test.</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我找到了一些例子,但没有一个真正奏效.我真的很感激一些帮助.

提前致谢.

Nic*_*los 14

您不需要iframe按照建议进行操作,但您应该在此处阅读有关对话框的文档.

相反,您需要在.open属性上加载内容-

$( "#openwindow" ).dialog({
 open: function(event, ui) {
   $('#divInDialog').load('test.html', function() {
     alert('Load was performed.');
   });
  }
});
Run Code Online (Sandbox Code Playgroud)

此外,您似乎使用.each同一个id-的id应该是在页内是唯一的.使用class来代替.

  • 如果URL在另一个域中,则不能使用`.load()`,因为跨域AJAX限制. (2认同)

The*_*pha 7

你可以试试这个

$(function(){
    $('a').on('click', function(e){
        e.preventDefault();
        $('<div/>', {'class':'myDlgClass', 'id':'link-'+($(this).index()+1)})
        .load($(this).attr('href')).appendTo('body').dialog();
    });
});
Run Code Online (Sandbox Code Playgroud)

上面的代码将创建一个新dialog的单击页面上的任何链接,并myDlgClass为每个对话框添加类名称和唯一ID link-1,link-2等等,但请记住,由于相同的源策略,只会加载页面链接而不是外部链接.

更新:

要使用外部站点链接,您可以使用iframe,这是使用iframe的示例.


PSR*_*PSR 0

您可以使用 iframe:

 $("#iframeId").attr("src", $(this).attr("href"));
 $('#dialogId').dialog('open');
Run Code Online (Sandbox Code Playgroud)
<div id="divId" >
    <IFRAME id="iframeId"  SRC="" width="" height = "" >
</div>
Run Code Online (Sandbox Code Playgroud)