如何覆盖由 jquery/javascript 设置的 css 高度?

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

how to overwrite css height set by jquery/javascript?

javascriptjquerycssheight

提问by frequent

I have this:

我有这个:

 var setHeight = $(this).outerHeight(); 
 // returns e.g. 687

 $("#someElement").css({'height': $setHeight+"px !important" });
 // I want to override this jquery-set height

I'm not sure this is the right way... probably not, since it's not working.

我不确定这是正确的方法……可能不是,因为它不起作用。

Thanks for helping out!

感谢您的帮助!

采纳答案by Charlotte

setHeight, not $setHeight. and the !importantis unneeded.

setHeight,不是$setHeight。并且!important是不需要的。

回答by Rory McCrossan

Your variable name doesn't have a leading $. Also, the !importantflag will cause this not to work in Firefox, however as you're applying this style directly to the element, you shouldn't need it.

您的变量名称没有前导$. 此外,该!important标志将导致这在 Firefox 中不起作用,但是当您将此样式直接应用于元素时,您不应该需要它。

$("#someElement").css('height', setHeight + "px");

Also note that if you're only setting the element's height you can also just shorten this to a height()call:

另请注意,如果您只设置元素的高度,您也可以将其缩短为height()调用:

$("#someElement").height(setHeight);

回答by faino

Your variables don't match; remember that punctuation and proper spelling are important to calling the variable properly; try changing the second line to:

您的变量不匹配;请记住,标点符号和正确拼写对于正确调用变量很重要;尝试将第二行更改为:

$("#someElement").css('height',setHeight+'px !important');

回答by John Shepard

Take out dollar sign ;) 'setHeight'

取出美元符号 ;) 'setHeight'

回答by brenjt

That should totally work except that your variable is setHeightand you are trying to use $setHeight. Make sure they are the same. Make sure your selector is correct and obtaining the element. I believe that you don't even need the !important. The style tag should overwrite any .css definition unless you have !importantin it.

除了您的变量是setHeight并且您正在尝试使用$setHeight. 确保它们相同。确保您的选择器正确并获取元素。我相信你甚至不需要!important. style 标签应该覆盖任何 .css 定义,除非你有!important它。