JavaScript style object and CurrentStyle object case study

JavaScript style object and CurrentStyle object case study

1. Style object

The style object represents a single style declaration and can be accessed from the document element to which the style is applied. The style object obtains the inline style , that is, the value of the style attribute in the element tag.

example:

<style type="text/css">#div{color:gray;}</div>//Internal style <div id="div" style="color:red;"></div>//Inline style document.getElementById('id').style.color;//The value is red

2. currentStyle object

Returns the final style of all style declarations (including internal, external, and inline) applied to the element according to the CSS cascading rules. Only IE and Opera support using CurrentStyle to get the calculated style of an element. The getComputeStyle() method can get the CSS attribute value used by the current element.

var div=window.getComputeStyle("div",null).color; //The first parameter is the target element, the second parameter is the pseudo-class (required, set to null if there is no pseudo-class)

The difference from the style object:

getComputeStyle() is read-only, it can only be obtained but not set, while style can be read and set;

For an element that does not have any style set, getComputedStyle() returns the value of the length property in the object, and the length in the style object is 0.

Different browsers have different support for the currentStyle object, so compatibility needs to be addressed.

var div = document.getElementById('div');

var colorStr=null;

if(div.currentStyle){//Compatible with IE

       colorStr=div.currentStyle;

}else{
colorStr=window.getComputedStyle(div,null);

}

var col=colorStr.color; //Get the color attribute value of div

3. Example (Draggable Layer)

CurrentStyle Object

style object

This is the end of this article about the detailed case analysis of JavaScript style object and CurrentStyle object. For more relevant content about js style object and CurrentStyle object, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JS gets CSS style (style/getComputedStyle/currentStyle)
  • Get the js function currentStyle(IE),defaultView(FF) of the style in the css style sheet
  • JavaScript reads styles other than inline (introduction to the differences between style, currentStyle, and getComputedStyle)
  • (currentStyle) Why can't javascript get the set CSS properties using style sometimes?

<<:  How to enable remote access permissions in MYSQL

>>:  LINUX Checks whether the port is occupied

Recommend

Implementation of react automatic construction routing

Table of contents sequence 1. Centralized routing...

Install tomcat and deploy the website under Linux (recommended)

Install jdk: Oracle official download https://www...

Simple example of HTML checkbox and radio style beautification

Simple example of HTML checkbox and radio style b...

Vue mobile terminal realizes finger sliding effect

This article example shares the specific code for...

How to obtain root permissions in a docker container

First, your container must be running You can vie...

WeChat applet component development: Visual movie seat selection function

Table of contents 1. Introduction 1. Component da...

Tomcat8 uses cronolog to split Catalina.Out logs

background If the catalina.out log file generated...

Basic installation process of mysql5.7.19 under winx64 (details)

1. Download https://dev.mysql.com/downloads/mysql...

Why MySQL does not recommend using subqueries and joins

To do a paginated query: 1. For MySQL, it is not ...

Each time Docker starts a container, the IP and hosts specified operations

Preface Every time you use Docker to start a Hado...

Echart Bar double column chart style most complete detailed explanation

Table of contents Preface Installation and Config...

How to connect to a remote server and transfer files via a jump server in Linux

Recently, I encountered many problems when deploy...

How to implement batch deletion of large amounts of data in MySQL large tables

The question is referenced from: https://www.zhih...

What is the length of a function in js?

Table of contents Preface Why How much is it? Num...

Implementing form submission without refreshing the page based on HTML

Using ajax to implement form submission without re...