javascript JQuery css max-height 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22337984/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:57:36  来源:igfitidea点击:

JQuery css max-height not working

javascriptjqueryhtmlcss

提问by Omid

I've tested this code for set .css('max-height','auto')css property in JQuery but it's not working, and when I set a dimension like .css('max-height','93px')it's working fine, how can I set it as auto?

我已经.css('max-height','auto')在 JQuery 中测试了设置css 属性的这段代码,但它不起作用,当我设置一个维度时,.css('max-height','93px')它工作正常,我该如何将其设置为auto

HTML:

HTML:

<div class="holder">
    <div>DIVISION</div>
    <div>DIVISION</div>
    <div>DIVISION</div>
    <div>DIVISION</div>
    <div>DIVISION</div>
    <div>DIVISION</div>
    <div>DIVISION</div>
</div>
<br />
<input type="button" onclick="test();" value="CHECK" />

CSS:

CSS:

.holder{
    max-height:40px;
    background:red;
    padding:10px;
    overflow:hidden;
}
.holder div{
    background:blue;
    margin:3px 0;
}

js:

js:

function test(){
    var hldr=$('.holder'); //get element

    alert('before set: ' + $('.holder').css('max-height')); //alert before set

    /* SET */
    hldr.css('max-height','auto');

    alert('after set: ' + $('.holder').css('max-height'));//alert after set
}

http://jsfiddle.net/silverlight/23UCV/2/

http://jsfiddle.net/silverlight/23UCV/2/

采纳答案by Omri Aharon

Changing max-heightto heightfixes your problem.

更改max-heightheight解决您的问题。

Demo

演示

回答by Ramkee

I have also faced the same problem.

我也遇到过同样的问题。

I have tried the maxHeightproperty. Now the problem has been resolved

我试过maxHeight属性。现在问题已经解决

$('#exampleId').css({"maxHeight":"100px"});

回答by pipepiper

Not sure if earlier browsers had this issue. Tested recently in Chrome 56.x, FF 51.0.x and IE 11, you can use:

不确定早期的浏览器是否有这个问题。最近在 Chrome 56.x、FF 51.0.x 和 IE 11 中测试,您可以使用:

max-height: none;

so this works

所以这有效

alert('after set: ' + $('.holder').css('max-height','none'));//alert after set

One can use this as well with jquery (tested with 2.0.3) and create a toggle

也可以将它与 jquery(用 2.0.3 测试)一起使用并创建一个切换

$('#someid').toggleClass("expand");

and css

和CSS

.expand{
   max-height: none !important;
}