Home > Web Front-end > JS Tutorial > body text

How to change the value of css display attribute in js

青灯夜游
Release: 2023-01-06 11:12:18
Original
10878 people have browsed it

JS method to change the css display attribute value: first use the "document.getElementById("id value")" statement to obtain the specified element object; then use the "element object.style.display="value"" statement to Just change the value of the display attribute.

How to change the value of css display attribute in js

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

HTML DOM allows JavaScript to change the style of HTML elements. So how does js change the value of the css display attribute?

Syntax:

Object.style.display="值";
Copy after login

Example:

Known :

<div class="login1" id="login">页面ID为login的元素</div>
<div class="login2" id="regist">页面ID为regist的元素</div>
Copy after login

Code:

<script>
document.getElementById("login").style.display = "none"; //(login为需要隐藏的页面ID)
document.getElementById("regist").style.display = "block" //(regist为需要显示的页面ID)
</script>
Copy after login

Description: Method to obtain Object element object:

1. Get element based on id

document.getElementById("id属性的值");
Copy after login

2. Get the element based on the tag name

document.getElementsByTagName("标签的名字");
Copy after login

3. Get the element based on the value of the name attribute

document.getElementsByName("name属性的值");
Copy after login

4. Get the element based on the class attribute

document.getElementsByClassName("类样式的名字");
Copy after login

5 .Get elements based on css path (get one)

document.querySelector("css路径");
Copy after login

6. Get elements based on css path (get a group)

document.querySelectorAll("css路径");
Copy after login

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to change the value of css display attribute in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template