在Javascript中,我可以检索元素的CSS值而不考虑其内联样式吗?
例:
body { font-size: 15px; }
<body style="font-size: 20px;">
Run Code Online (Sandbox Code Playgroud)
有没有办法可以检索"15px"而不是"20px"?
当然是!只需删除style属性,使用getComputedStyle()并添加style属性:
//Get the body's style attribute:
var bodyStyle = document.body.getAttribute("style");
//Now, get rid of it:
document.body.removeAttribute("style");
//Now use getComputedStyle() to compute the font size of the body and output it to the screen:
document.write( getComputedStyle(document.body).fontSize );
//Now add the style attribute back:
document.body.setAttribute("style", bodyStyle);Run Code Online (Sandbox Code Playgroud)
body { font-size: 15px; }Run Code Online (Sandbox Code Playgroud)
<body style="font-size: 20px;">
</body>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
111 次 |
| 最近记录: |