2017-09-17 137 views

回答

0

使用window.getComputedStyle(mySpan).lineHeight可以获取元素行高的值,而不管样式是内联还是外部CSS文件。

var mySpan=document.getElementById("mySpan"); 
 
console.log(window.getComputedStyle(mySpan).lineHeight);
<span id="mySpan" style="line-height:200px;">hello world</span>

1

带有-的CSS属性在Javascript对象的camelCase中表示。例如 - mySpan.style.lineHeight

您还可以使用括号表示法来访问属性。防爆 - mySpan.style['line-height']

var mySpan=document.getElementById("mySpan"); 
 
console.log(mySpan.style.lineHeight); 
 
console.log(mySpan.style['line-height']);
<span id="mySpan" style="line-height:200px;">hello world</span>

+0

行高不等于height.How我可以得到高度? – JackieWillen

+0

你可以使用'window.getComputedStyle(mySpan).height' –