Javascript 使用javascript设置最大高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2244773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set max-height using javascript
提问by Vinay Pandey
I have a div, and the maximum width for this div is user defined. I know I can get it done using element.style.height but this doesn't work in IE.
我有一个 div,这个 div 的最大宽度是用户定义的。我知道我可以使用 element.style.height 来完成它,但这在 IE 中不起作用。
Any ideas on how to implement the max-height equivalent of Firefox by using javascript?
关于如何使用 javascript 实现与 Firefox 等效的最大高度的任何想法?
回答by GodsBoss
Usually style attribute names are translated into javascript property names by removing the hyphens and camelcase the name instead.
通常通过删除连字符和驼峰命名将样式属性名称转换为 javascript 属性名称。
So background-colorbecomes backgroundColor, text-alignbecomes textAlignand max-heightbecomes maxHeight.
所以background-color成为backgroundColor,text-align成为textAlign,max-height成为maxHeight。
You can set an element el's maximum height to mHeightby:
您可以将元素el的最大高度设置mHeight为:
el.style.maxHeight=mHeight;
el.style.maxHeight=mHeight;
Remember to use a valid value for mHeight.
请记住对 使用有效值mHeight。
回答by rahul
document.getElementById ( "yourelementid" ).style.maxHeight = "100px";
请参阅maxHeight 属性
maxHeight was introduced in Windows Internet Explorer 7
maxHeight 是在 Windows Internet Explorer 7 中引入的
回答by B-A
To make the animation time match what is expected, I would use el.scrollHeightto find the required height of the element.
为了使动画时间符合预期,我将使用el.scrollHeight来查找元素所需的高度。

