手动创建 jQuery 对话框检查是否需要调整大小

发布于 2024-09-29 21:22:41 字数 880 浏览 0 评论 0原文

我有一个 jQuery 对话框,它在打开后通过 ajax 加载其内容。

看这个 JSFiddle (内容不是通过ajax加载的,而是在打开后添加的,这足以证明我的问题)。

html:

<div id="test">test</div>

js:

$("#test").dialog({
    minHeight:100,
    maxHeight:200,
    width:300,
    open: function(){
        $(this).html("test<br /><br />test<br />" +
                     "<br />test<br /><br />test" +
                     "<br /><br />test<br /><br />" +
                     "test<br /><br />test<br />" +
                     "<br />test<br /><br />test");
    }
});

当它打开时,它不会保持其最大高度,直到您调整对话框的大小。将内容添加到对话框后,是否可以调用某种resize方法?

我不想手动确定是否需要调整大小以及调整多高,因为它几乎已经内置在 ui 插件中。

I have a jQuery dialog that loads its content trough ajax after its opened.

See this JSFiddle (content is not loaded trough ajax but added after open, which is enough to demonstrate my problem).

html:

<div id="test">test</div>

js:

$("#test").dialog({
    minHeight:100,
    maxHeight:200,
    width:300,
    open: function(){
        $(this).html("test<br /><br />test<br />" +
                     "<br />test<br /><br />test" +
                     "<br /><br />test<br /><br />" +
                     "test<br /><br />test<br />" +
                     "<br />test<br /><br />test");
    }
});

When it is opened it does not stay true to its maxHeight until you resize the dialog. Is there some sort of resize method I can call after I added content to the dialog?

I prefer not to have to manually figure out if a resize is needed and how high since its pretty much already built into the ui plugin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水染的天色ゝ 2024-10-06 21:22:41

将 html 代码放在 webapge 上的实际 div 中,然后在该 div 上使用对话框。
或者在初始化后更改高度(和其他选项)...来自 jquery docs

//getter
var width = $( ".selector" ).dialog( "option", "height" );
//setter
$( ".selector" ).dialog( "option", "height", 460 );

Put the html code in an actual div on the webapge, then use dialog on that div.
Or to change height (and other options) after init ... from jquery docs :

//getter
var width = $( ".selector" ).dialog( "option", "height" );
//setter
$( ".selector" ).dialog( "option", "height", 460 );
说谎友 2024-10-06 21:22:41

JSFiddle 示例。

这并不完全是我所希望的优雅解决方案,但到目前为止它似乎有效。

如果有人有更好的解决方案,我会很高兴听到。

JSFiddle example.

Not exactly the elegant solution I hoped for but so far it seems to work.

If anybody has a better solution I'll be glad to hear it.

情丝乱 2024-10-06 21:22:41

在 open 函数中设置 maxHeight 似乎可以解决问题:

$("#test").dialog({
    minHeight:100,
    width:300,
    open: function(){
        $(this).html("test<br /><br />test<br /><br />test<br /><br />test<br /><br />test<br /><br />test<br /><br />test<br /><br />test<br /><br />test");
        $(this).parent().css("height", "auto");
        $(this).css("maxHeight", 200);
    }
});

编辑:当“测试”div 用作对话框时,它会与标题 div 一起嵌套在对话框 div 中。因此,测试 div 及其包含的 div 都需要使用包含的 div 设置高度,同时考虑到对话框标题 div 的高度。将其高度设置为自动应该可以解决这个问题。

Setting the maxHeight in the open function seems to do the trick:

$("#test").dialog({
    minHeight:100,
    width:300,
    open: function(){
        $(this).html("test<br /><br />test<br /><br />test<br /><br />test<br /><br />test<br /><br />test<br /><br />test<br /><br />test<br /><br />test");
        $(this).parent().css("height", "auto");
        $(this).css("maxHeight", 200);
    }
});

EDIT: It appears when the 'test' div is used as a dialog it becomes nested within the dialog div along with a header div. As a result both the test div and its containing div need to have their heights set with the containing div taking into account the height of the dialog header div. Setting its height to auto should take care of this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文