2012-02-06 51 views
-4

有人可以帮我设置jQuery UI对话框自动打开吗?谢谢。我当前的代码:jQuery UI自动打开

<script type="text/javascript"> 
function openDialog(url) { 
    $("<div class='popupDialog'>Loading...</div>") 
     .dialog({ 
      autoOpen: true, 
      closeOnEscape: true, 
      width: 'auto', 
      height: 'auto', 
      modal: true, 
      beforeClose: function(){ $(this).remove(); } 
     }).bind('dialogclose', function() { 
      jdialog.dialog('destroy'); 
     }).load(url, function() { 
      $(this).dialog("option", "position", ['center', 'center']); 
       $(this).dialog("open"); 
     }); 
} 

$(window).resize(function() { 
    $(".ui-dialog-content").dialog("option", "position", ['center', 'center']); 
}); 
</script> 
+2

有什么理由不与您现有的代码的工作?我可以猜测你的对话框没有打开,但它可能是别的。检查您的浏览器的控制台是否有任何代码错误。 – Bojangles 2012-02-06 20:42:22

回答

1

我认为这是你在找什么:

$(document).ready(function() { //All jQuery code that affects the DOM should run after the DOM is in a readyState. 
    'use strict'; //ECMAScript 5, yay! 
    var url = 'www.somsite.com'; 
    function openDialog(url) { 
     //added return statement to return the dialog 
     //Using "<div>content</div>" as the "selector" creates a new div element that is not attached to the DOM. 
     return $('<div class="popupDialog">Loading...</div>').dialog({ 
      'autoOpen': true, 
      'closeOnEscape': true, 
      'width': 'auto', 
      'height': 'auto', 
      'modal': true, 
      'beforeClose': function() { 
       $(this).remove(); 
      } 
     }).bind('dialogclose', function() { 
      $(this).dialog('destroy'); 
     }).load(url, function() { 
      $(this).dialog('option', 'position', ['center', 'center']); 
      $(this).dialog('open'); 
     }); 
    } 
    $('body').append(openDialog(url)); //Append created DOM element to the DOM 
    $(window).resize(function() { 
     $('.ui-dialog-content').dialog('option', 'position', ['center', 'center']); 
    }); 
});