[英]Setting font-family via style.font doesn't work unless font size is also set
I want to set the font of a bit of text through some JavaScript. 我想通过一些JavaScript设置一些文本的字体。 For some reason, it seems that I can only do so when setting the
font-size
as well. 出于某种原因,似乎我也只能在设置
font-size
时这样做。 So this works: 这样可行:
el.style.font = "10px arial, serif"
but this doesn't 但事实并非如此
el.style.font = "arial, serif"
( jsfiddle example ) ( jsfiddle例子 )
I know there are other ways to set the font, but for other reasons this approach would be convenient -- why isn't it working? 我知道还有其他方法可以设置字体,但由于其他原因,这种方法很方便 - 为什么它不起作用?
I've checked both Firefox and Chrome, so I assume it's something I don't understand as opposed to a bug. 我已经检查了Firefox和Chrome,所以我认为这是我不理解的东西而不是bug。
Because according to the definition from W3C, font-size
must be there if you include font-family
. 因为根据W3C 的定义 ,如果包含
font-family
,则必须存在font-size
。
[
[ <‘font-style’> || <font-variant-css21> || <‘font-weight’> || <‘font-stretch’> ]?
<'font-size'>
[ / <'line-height'> ]?
<'font-family'>
] |
caption |
icon |
menu |
message-box |
small-caption |
status-bar
Edit: Updated to CSS3 definition. 编辑:已更新为CSS3定义。
你错误的font
为font-family
(在JS-speak中的fontFamily
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.