使用 jQuery .css() 和 em 更改宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13582518/
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
Changing width using jQuery .css() and em
提问by TD_Nijboer
I need to change the width of an div using jQuery.
我需要使用 jQuery 更改 div 的宽度。
When I use the following, it works:
当我使用以下内容时,它可以工作:
$('#footer').css('width', '100%');
$('#footer').css('width', '-=239');
When I use this, it works:
当我使用它时,它有效:
$('#footer').css('width', '100%');
$('#footer').css('width', '-=239px');
But when I use this it doesn't do anything:
但是当我使用它时它什么也不做:
$('#footer').css('width', '100%');
$('#footer').css('width', '-=21em');
Is there a way to make jQuery work with em
? or to calculate em
to px
and set a variable for example and subtract that value?
有没有办法让 jQuery 工作em
?或计算em
,以px
及设置一个变量例如和减去该值?
edit: thanks everybody for correcting my spelling and code!
编辑:感谢大家更正我的拼写和代码!
回答by The Mechanic
i tried it and it is working i think you should check your code i have checked it in all browser and it is working fine px and em both working well you can use this code put this code in a dummy page and u can see this works
我试过了,它正在工作 我认为你应该检查你的代码
<style>
#footer{
background: black;
height:50px;
}
</style>
<script type="text/javascript">
$(function(){
$('#footer').css('width', '100%');
$('#footer').css('width', '-=210em');
});
</script>
<div id="footer"></div>