Home >Web Front-end >JS Tutorial >What is the method to control the hiding of elements in javascript

What is the method to control the hiding of elements in javascript

王林
王林Original
2021-11-03 16:53:007145browse

The method for JavaScript to control the hiding of elements is to set the display in the element's style attribute, such as [var t = document.getElementById('test');t.style.display = 'none';].

What is the method to control the hiding of elements in javascript

#The operating environment of this article: windows10 system, javascript 1.8.5, thinkpad t480 computer.

We have two ways to hide elements in javascript:

Method 1: Set the display in the element style attribute

var t = document.getElementById('test');//选取id为test的元素
t.style.display = 'none';// 隐藏选择的元素
t.style.display = 'block';// 以块级样式显示

Method 2: Set the element in the style attribute visibility

var t = document.getElementById('test');
t.style.visibility = 'hidden';// 隐藏元素
t.style.visibility = 'visible';// 显示元素

The difference between these two methods:

The original position will not be occupied after setting display to hide, but the element position will still be occupied after hiding through visibility.

Recommended learning: javascript video tutorial

The above is the detailed content of What is the method to control the hiding of elements in javascript. 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