如何在单个div中启用滚动条?

4

我有一个看起来像这样的布局:

<html>
  <head>
    stuff here
  </head>
  <body>
    <div id="master">
      <div id="toolbar">
        <input type="text" id="foo">
      </div>
      <div id="content">
        a whole bunch of content in here
      </div>
    </div>
  </body>
</html>

'#master'是jquery UI对话框的容器。我希望'#content' div的内容可以滚动,但'#toolbar'不可滚动。使用jquery UI的对话框可以实现吗?

2个回答

10

只需使用CSS规则:

#content { overflow: auto; height: [desired height] }

当模态框拥有动态高度时,可能需要使用jQuery。在这种情况下,在打开模态框时,您可以设置内部容器的高度。例如:

open

open: function(){
    var modalHeight = $('#master').height();
    $('#content').height(modalHeight - [footer and title]);
}

“open:”是在对话框打开时调用的函数吗? - gdanko
太好了!这是我的第一个JS项目,所以我还在学习中。我感谢社区的耐心。 :) - gdanko
这个可行,但如果我放大窗口,对话框底部会拉伸,而#content的高度并不会改变。如果我缩小窗口,会添加第二个滚动条。我想我知道为什么了...嗯 - gdanko

0
使用CSS为#content div设置固定的高度和可选的宽度,并在该div上设置CSS属性overflow: auto。如果内容超过高度,将会出现滚动条。

另外:请勿在对话框的构造函数选项中设置高度。 - lsuarez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接