我很难找到这方面的具体信息和例子.我的应用程序中有许多jQuery UI对话框连接到加载了的div.ajax()调用.它们都使用相同的设置调用:

 $(".mydialog").dialog({
        autoOpen: false,
        resizable: false,
        modal: true
 });

I just want to have the dialog resize to the width of the content that gets loaded. Right now, the width just stays at 300px (the default) and I get a horizontal scrollbar.

据我所知,"自动调整大小"不再是对话框的选项,当我指定它时,什么也不会发生.

I'm trying to not write a separate function for each dialog, so .dialog("option", "width", "500") is not really an option, as each dialog is going to have a different width.

Specifying width: 'auto' for the dialog options just makes the dialogs take up 100% of the width of the browser window.

What are my options? I'm using jQuery 1.4.1 with jQuery UI 1.8rc1. It seems like this should be something that is really easy.

编辑:我已经为此实现了一个笨拙的解决方案,但我仍在寻找更好的解决方案.

推荐答案

I've just wrote a tiny sample app using JQuery 1.4.1 and UI 1.8rc1. All I did was specify the constructor as:

var theDialog = $(".mydialog").dialog({
        autoOpen: false,
        resizable: false,
        modal: true,
        width:'auto'
});

我知道你说过这会占据浏览器窗口的100%宽度,但在FF3.6、Chrome和IE8上测试过后,这里的效果很好.

I'm not making AJAX calls, just manually changing the HTML of the dialog but don't think that will cause any probs. Could some other css setting be knocking this out?

The only problem with this is that it makes the width off-centre but I found this support ticket where they supply a workaround of placing the dialog('open') statement in a setTimeout to fix the problem.

Here is the contents of my head tag:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery-ui.min.js"></script>
<link href="jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
    $(function(){
        var theDialog = $(".mydialog").dialog({
            autoOpen: false,
            resizable: false,
            modal: true,
            width: 'auto'
        });

        $('#one').click(function(){
            theDialog.html('some random text').dialog('open');
        });

        $('#two').click(function(){
            theDialog.html('<ul><li>Apple</li><li>Orange</li><li>Banana</li><li>Strawberry</li><li>long text string to see if width changes</li></ul>').dialog('open');
        });

        $('#three').click(function(){
            //this is only call where off-centre is noticeable so use setTimeout
            theDialog.html('<img src="./random.gif" width="500px" height="500px" />');
            setTimeout(function(){ theDialog.dialog('open') }, 100);;
        });
     });
</script>

I downloaded the js and css for Jquery UI from http://jquery-ui.googlecode.com/files/jquery-ui-1.8rc1.zip. and the body:

<div class='mydialog'></div>
<a href='#' id='one'>test1</a>
<a href='#' id='two'>test2</a>
<a href='#' id='three'>test3</a>

Jquery相关问答推荐

通过 jQuery 提取 application/json 数据

加载外部 css 文件,如 jquery 中的脚本,这在 ie 中也兼容

AngularJS 中的 ScrollTo 函数

SCRIPT7002:XMLHttpRequest:网络错误 0x2ef3,由于错误 00002ef3 无法完成操作

Jquery .on('scroll')在滚动时不触发事件

jQuery/JavaScript 碰撞检测

jQuery 序列化不注册复选框

jQuery中追加的相反

禁用表单提交上的提交按钮

Jquery,清除/清空 tbody 元素的所有内容?

使用jquery设置di​​v标签的值

如何使用 jQuery 为文本对齐动态添加样式

jQuery '如果 .change() 或 .keyup()'

将片段添加到 URL 而不导致重定向?

如何在 JavaScript 中的对象数组中查找值?

jquery可排序占位符高度问题

Google 的 CDN 上的最新 jQuery 版本

jQuery.active 函数

jquery $(window).width() 和 $(window).height() 在未调整视口大小时返回不同的值

如何删除集中的 contenteditable pre 周围的边框?