Home Web Front-end JS Tutorial Summary of three methods to set element class in js_javascript skills

Summary of three methods to set element class in js_javascript skills

May 16, 2016 pm 06:02 PM
class element

1. el.setAttribute('class','abc');
Copy code The code is as follows:

<!DOCTYPE HTML>
<HTML>
<HEAD>
<meta charset="utf-8" />
< title>setAttribute('class', 'abc')</title>
<style type="text/css">
.abc {
background: red;
}
</style>
</HEAD>
<BODY>
<div id="d1">test div</div>
<script>
var div = document.getElementById('d1');
div.setAttribute("class", "abc");
</script>
</BODY>
< /HTML>

IE6/7: The background color of the div is not red
IE8/9/10/Firefox/Safari/Chrome/Opera: The background color of the div is red
Result: IE6/ 7 does not support the setAttribute('class',xxx) method to set the class of an element.
2. el.setAttribute('className', 'abc')
Copy code Code As follows:

<!DOCTYPE HTML>
<HTML>
<HEAD>
<meta charset="utf-8" />
<title>setAttribute('className', 'abc')</title>
<style type="text/css">
.abc {
background: red;
}
</style>
</HEAD>
<BODY>
<div id="d1">test div</div>
<script>
var div = document.getElementById('d1');
div.setAttribute("className", "abc");
</script>
</BODY>
</HTML>

IE6/7: The background color of the div is red
IE8/9/10/Firefox/Safari/Chrome/Opera: The background color of the div is not red
Result: IE8/9/10/Firefox/Safari/Chrome/Opera does not support the setAttribute('className',xxx) method to set the class of an element.
It’s interesting that when using setAttribute, the first parameters are class and className. The situation is exactly the opposite in IE6/7 and IE8/9/10/Firefox/Safari/Chrome/Opera.
3. el.className = 'abc';
Copy code The code is as follows:

<!DOCTYPE HTML>
<HTML>
<HEAD>
<meta charset="utf-8" />
<title> el.className = 'abc'</title>
<style type="text/css">
.abc {
background: red;
}
</ style>
</HEAD>
<BODY>
<div id="d1">test div</div>
<script>
var div = document .getElementById('d1');
div.className = 'abc';
</script>
</BODY>
</HTML>

Supported by all browsers.
Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

Replace the class name of an element using jQuery

In JavaFX, what are the different path elements? In JavaFX, what are the different path elements? Aug 28, 2023 pm 12:53 PM

In JavaFX, what are the different path elements?

Methods of predefined Class objects in java Methods of predefined Class objects in java Jul 01, 2023 pm 06:41 PM

Methods of predefined Class objects in java

Detailed explanation of PHP Class usage: Make your code clearer and easier to read Detailed explanation of PHP Class usage: Make your code clearer and easier to read Mar 10, 2024 pm 12:03 PM

Detailed explanation of PHP Class usage: Make your code clearer and easier to read

What elements are not supported by html5 What elements are not supported by html5 Aug 11, 2023 pm 01:25 PM

What elements are not supported by html5

CSS transition effect: how to achieve the sliding effect of elements CSS transition effect: how to achieve the sliding effect of elements Nov 21, 2023 pm 01:16 PM

CSS transition effect: how to achieve the sliding effect of elements

C++ program: add an element to an array C++ program: add an element to an array Aug 25, 2023 pm 10:29 PM

C++ program: add an element to an array

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Aug 26, 2023 pm 10:58 PM

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it?

See all articles