jquery - 设置浮动div的动态等高度
我有 2 个 div 容器,左侧有一个导航,右侧有一个内容:
#leftnav_static
{
padding:5px;
margin-top: 5px;
margin-left: 5px;
height: 1000px;
width: 195px;
font-size: 1.35em;
float:left;
background-image: url('pagenav.jpg');
}
#content_dynamic
{
margin-top: 5px;
margin-left: 215px;
height: auto;
width: 700px;
padding: 5px;
background-image: url('pagenav.jpg');
font-size: 1em;
line-height:1.6em;
white-space:nowrap;
}
现在我想将 leftnav 设置为与内容相同的高度(如果可能的话,不要使用假列):
$('#leftnav_static').height($("#content_dynamic").height());
或者
$('#leftnav_static').innerHeight($("#content_dynamic").innerHeight());
似乎不起作用。
关于为什么会这样的任何建议吗?
I have 2 div containers, one navigation on the left, one content right to that:
#leftnav_static
{
padding:5px;
margin-top: 5px;
margin-left: 5px;
height: 1000px;
width: 195px;
font-size: 1.35em;
float:left;
background-image: url('pagenav.jpg');
}
#content_dynamic
{
margin-top: 5px;
margin-left: 215px;
height: auto;
width: 700px;
padding: 5px;
background-image: url('pagenav.jpg');
font-size: 1em;
line-height:1.6em;
white-space:nowrap;
}
Now I want to set leftnav to the same height as content (no faux columns if possible):
$('#leftnav_static').height($("#content_dynamic").height());
or
$('#leftnav_static').innerHeight($("#content_dynamic").innerHeight());
dont seem to work.
any suggestions as to why that is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它确实有效。请参阅此 jsfiddle。
您是否在 jQuery 就绪块中运行代码?此外,如果您想通过浏览器“缩放”中的文本大小更改来维持这种高度关系,则需要响应调整大小事件。如果在某些时候您将
content_dynamic
div 的宽度设置为 auto,则当content_dynamic
div 的高度发生变化时,您还需要调整侧边栏 div 的大小(同样,通过响应调整大小事件)。jQuery 只允许您附加到窗口级别的调整大小事件,但有一些插件可以轻松地将其转换为 div 级别调整大小事件。
HTML:
CSS:
JavaScript:(假设 jQuery 库已加载)
注意:与人造列或其他纯 CSS 方法相比,您无需担心缩放或调整大小,而且您的网站将适合关闭 JavaScript 的用户。
It does work. See this jsfiddle.
Are you running the code in a jQuery ready block? Also, if you want to maintain this height relationship through text size changes from browser 'zooms', you will need to respond to resize events. If at some point you make your
content_dynamic
div have a width of auto, you will also need to resize the sidebar div when the height of thecontent_dynamic
div changes (again, by responding to a resize event).jQuery only allows you to attach to a resize event at the window level, but there are plugins that ease translating that to a div level resize event.
HTML:
CSS:
JavaScript: (Assuming that the jQuery library is already loaded)
Note: The benefit of a faux columns or other pure CSS approaches is that you don't need to worry about zooming or resizes as much--and your site would work for people that have JavaScript turned off.
你必须明白你正在操纵 CSS 属性。
应该可以解决问题。
You have to understand that you are manipulating CSS attributes.
should do the trick.
将显示块添加到 #leftnav_static
...这将起作用
请参阅我的示例; http://jsfiddle.net/D3gTy/
Add display block to #leftnav_static
...and this will work
See my example; http://jsfiddle.net/D3gTy/