Native JS gets HTML styles and modifies them
Everyone knows that getting the style of an element in JavaScript native operation is a relatively common thing in actual operation. Here I would like to introduce the method of getting the css style. I hope it can help some people in need. If you are lucky, If someone sees a better way, you are welcome to suggest it! ! !
1. Get the inline element style:
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>js获取外部样式</title></head><body> <div id="div" style="width:200px;height:200px;border:3px solid cyan;"></div></body><script> //获取行间样式 var div = document.getElementById(‘div‘); var width = div.style.width; alert(width);//200px</script></html>
In the JS code, alert will pop up the width of the div element as 200px; the purpose of getting the width of the element is achieved, but this simple method It is only suitable for obtaining the inline element style of the element, and cannot obtain the internal style and external style. In the project, the solution of writing inline elements is not recognized by everyone, so this method is just for everyone to understand.
2. Obtaining non-interline style elements:
If the element style is not interline style, the above method cannot be used to obtain the element style, and another method needs to be used:
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>js获取内部部样式</title> <style> #div{ width: 200px; height: 200px; border:3px solid cyan; } </style></head><body> <div id="div""></div></body><script> var div = document.getElementById(‘div‘); //获取div的width var width = window.getComputedStyle(div,null).width; alert(width);//200px //修改div的width div.style.cssText=‘width:300px;‘</script></html>
In the above code, window.getComputedStyle('Element',null).'Style' is used. The compatibility of this method: Firefox and Google IE9 obtain the calculated style, but what needs to be noted here is the use The value obtained by this method is in read-only mode and cannot be modified, so the style.cssText method is used to modify its properties. Here you need to pay attention to the writing method.
3. Compatibility:
The unspoken rule in the previous paragraph is that all good things cannot be used universally. Yes, you guessed it right, the willful IE cannot use the above method, but IE has its own method called currentStyle. The usage is the same, so I won’t go into details. I will write it directly below. The compatible code that has been processed is as follows:
var div = document.getElementById(‘div‘);if (div.currentStyle) { console.log(div.currentStyle.width); }else{ console.log(getComputedStyle(div,null).width); } 方法封装: function getStyle(obj,name){ if (obj.currentStyle) { return obj.currentStyle[name]; }else{ return getComputedStyle(obj,false)[name]; } }
Native JS gets the HTML style and modifies it
For more native JS to get the HTML style and modifies it, please pay attention to the PHP Chinese website for related articles!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.
