【问题标题】:jQuery UI Dialog using iframe URL使用 iframe URL 的 jQuery UI 对话框
【发布时间】:2013-01-12 06:19:00
【问题描述】:

我使用 nyroModal 和 Fancybox 作为网站工具,但在这种情况下,我必须使用 jQuery UI 的对话框工具。我需要这个对话框来加载页面。我相信我以前做过这个,但我遇到的一切似乎都比它应该的复杂。我不能使用类似...

$( "#dialog" ).dialog({
      autoOpen: false,
      modal: true,
      url: http://www.google.com
      });

<button id="dialog">Open Dialog</button>

并在一个简单的 iframe 中打开页面?提前致谢。


我确实发现我有这个代码,

<script>
  //$.fx.speeds._default = 500;  
  $(function() {    
    $( "#dialog" ).dialog({      
    autoOpen: false,      
    show: "fade",   
    hide: "fade",
            modal: true,            
            open: function () {$(this).load('nom-1-dialog-add-vessels.html');},                     
            height: 'auto',            
            width: 'auto',        
            resizable: true,    
            title: 'Vessels'    });     

    $( "#opener" ).click(function() {      
    $( "#dialog" ).dialog( "open" );      
    return false;   
    });  
  });  
  </script>

<div id="dialog"></div><button id="opener">Open Dialog</button>

但它没有加载实际页面。

【问题讨论】:

    标签: javascript iframe dialog modal-dialog


    【解决方案1】:

    我尝试了类似的事情。检查这个http://jsfiddle.net/P2Q5U/

    <div id="dialogContent" title="Basic dialog">
      <iframe src="http://www.w3schools.com"></iframe>
    </div>
    <button id="dialog">Open Dialog</button>
    
     $(function () {
       $("#dialogContent").dialog({
         autoOpen: false,
         modal: true
       });
    
       $('#dialog').click(function () {
         $("#dialogContent").dialog( "open" );
       });
     });
    

    【讨论】:

    • 可以动态调整大小吗?
    • 您需要指定对话框和 iframe 的大小。
    【解决方案2】:

    url 不是jQuery UI dialog 中的选项之一。

    对我有用的一件事是在您的对话框 div 中添加一个 iframe,并在 open 事件上设置其 url 属性。

    喜欢:

    <div id="dialog">
        <iframe id="myIframe" src=""></iframe>
    </div>
    <button id="dialogBtn">Open Dialog</button>
    

    还有 JS:

    $("#dialog").dialog({
        autoOpen: false,
        modal: true,
        height: 600,
        open: function(ev, ui){
                 $('#myIframe').attr('src','http://www.jQuery.com');
              }
    });
    
    $('#dialogBtn').click(function(){
        $('#dialog').dialog('open');
    });
    

    不过,您会发现需要在 iframe 上进行一些样式设置以使其看起来不错。

    #myIframe{
      height: 580px;
    }
    

    编辑:工作版本 - http://jsbin.com/uruyob/1/edit

    【讨论】:

    • 这似乎有道理,但我的页面上有一个空框。
    • js bin eg 看起来如此简单易行,但我的页面上已经出现了一个空框,并且文本“你的 iframe 下面”就在它旁边,当我单击按钮时没有任何反应!我还尝试包含所有 jquery-ui-js 文件。什么都没有!
    • 您可能希望将 type='button' 添加到按钮。否则,如果您的按钮在表单中,它将提交。
    • 我可以在 Blazor 服务器应用上做同样的事情吗?
    【解决方案3】:

    根据Floyd Pink 和您的代码,我已经合并了一个代码。在这里查看http://jsfiddle.net/Nz9Q8/

     $(function () {
      $("#dialog").dialog({
        autoOpen: false,
        show: "fade",
        hide: "fade",
        modal: true,
        open: function (ev, ui) {
          $('#myIframe').src = 'http://www.w3schools.com';
        },
        height: 'auto',
        width: 'auto',
        resizable: true,
        title: 'Vessels'
      });
    
      $("#opener").click(function () {
        $("#dialog").dialog("open");
        return false;
      });
    });
    

    【讨论】:

    • 您可以从 iframe 中删除 src 并将这一行 $('#myIframe').src = 'http://www.w3schools.com'; 更改为 $('#myIframe').attr('src','http://www.w3schools.com');
    • @FloydPink 谢谢。有道理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多