当我尝试使用以下代码使用 IE 或 Firefox 获取元素的整个字体样式时,我只得到一个空结果,但是使用 Chrome 和 Opera 我得到 "normal normal bold normal 20px/normal arial"
如我所料。
<!-- HTML -->
<div id="test" style="font: bold 20px arial; color: red;">test</div>
// JS
alert($('#test').css('font'));
为什么会发生这种情况,否则我该如何获得完整的字体属性?
fiddle : http://jsfiddle.net/mwj12xkv/
最佳答案
您必须查询您感兴趣的各个font-*
属性。来自the css
documentation :
Retrieval of shorthand CSS properties (e.g.,
margin
,background
,border
), although functional with some browsers, is not guaranteed. For example, if you want to retrieve the renderedborder-width
, use:$( elem ).css( "borderTopWidth" )
,$( elem ).css( "borderBottomWidth" )
, and so on.
关于javascript - 使用 jQuery 在 IE 和 Firefox 中获取字体 css 属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27523507/