Home  >  Article  >  Web Front-end  >  How to get the style in the style sheet in js (code)

How to get the style in the style sheet in js (code)

不言
不言Original
2018-08-23 14:50:182701browse

The content of this article is about how js obtains the styles (code) in the style sheet. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

We all know that js cannot directly obtain the styles in the style sheet, such as width and left. It can only directly obtain the inline styles. However, it is currently not recommended to write inline styles in the mainstream, so we encapsulate a method that can directly obtain the non-inline styles. The style function

<span style="font-size:18px;"> 
function getStyle(obj,name){     
if(obj.currentStyle){          
return obj.currentStyle[name];
     }     
     else{        
      return getComputedStyle(obj,false)[name];  //false是一个无用的参数
     }
 }
 </span>

getStyle function has 2 parameters. The first parameter obj is the object to be obtained, and the second parameter name is the attribute to be obtained, and has been processed for compatibility. currentStyle is for IE browser , getComputedStyle is for Firefox browser.

Related recommendations:

What are the return data types of typeof in js? Summary of js typeof return type and type conversion

Comparison of the differences between json and XML

The above is the detailed content of How to get the style in the style sheet in js (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn