5

我创建了一个 div,然后在对话框中打开它。我尝试设计这个 div 以实现内容在两个方向上都扩展为 100%。

到目前为止我尝试了什么:设置 div 样式

#content {
    width:auto !important;
    height:auto !important;
    background-color:red !important;
    display:block !important;
}

并设置默认的 div 样式

$(div).dialog({ dialogClass: 'someStyle' });

.someStyle .ui-dialog-content 
{
    height:auto !important;
    display:block !important;
}

似乎没有任何效果!我覆盖了宽度属性,但不能覆盖高度属性。如何破解这个?

我想实现这样的目标:http: //jsfiddle.net/S3FQv/

4

3 回答 3

13

您可以使用 jQuerywidthheight属性来获取视口的像素宽度并将它们作为样式应用到 div

var w = $(window).width();
var h = $(window).height();

//Set up the dialog with the width and height set above.
$('#myDialog').dialog({
    title: 'My Dialog',
    height: h,
    width: w
});

看到这个小提琴:http: //jsfiddle.net/URpmR/2/

如果用户更改浏览器的大小,您还应该添加一些额外的代码来重新执行该函数:

$(window).resize(myFunction());
于 2013-02-28T10:40:57.917 回答
1

为了使内容具有 100% 的高度,必须在htmlandbody标签上指定此高度:

html, body { height:100%; margin:0; padding:0; }

此外,设置auto并不一定意味着宽度和/或高度将为 100%。同样,旧版本的 IE 不支持width: auto;height: auto;

如果可能的话,你也不应该用它!important来覆盖现有的样式。

JSFiddle 示例

于 2013-02-28T10:32:04.717 回答
0

width选项设置'auto'为在此答案中找到

于 2016-06-06T23:33:58.893 回答