Html document getelementbyid style display none

Javascript DOM Style display Property

The display property gets and sets the element’s display type.

The HTML elements are mostly «inline» or «block» elements:

  • An inline element has floating content on its left and right side.
  • A block element fills the entire line.

In other words, an inline element can share the same line with other elements, while the block element always starts a new line.

The display property can show or hide an element.

It is similar to the visibility property.

If you set display:none, it hides the entire element and removes it from the layout, while visibility:hidden means that the element will be invisible, but the element occupy in its original position and size.

If an element is a block element, its display type can also be changed with the float property.

Browser Compatibility

Syntax

Return the display property:

let a = object.style.display;
object.style.display = value;

Property Values

Value Element is rendered as
block a block-level element
compact

Default Value

Return Value

A String, representing the display type of an element.

More Examples

Difference between the display property and the visibility property:

function demoDisplay() < document.getElementById("myP1").style.display = "none"; > function demoVisibility() < document.getElementById("myP2").style.visibility = "hidden"; >
!DOCTYPE html> html> body> p id="myP1">This is some text. p id="myP2">This is some text. input type="button" onclick="demoDisplay()" value="Hide text with display property"> input type="button" onclick="demoVisibility()" value="Hide text with visibility property"> script> function demoDisplay() !-- w w w . d e m o 2 s . c o m--> document.getElementById("myP1").style.display = "none"; > function demoVisibility() < document.getElementById("myP2").style.visibility = "hidden"; >   

Example

Toggle between hiding and showing an element:

function myFunction() < let x = document.getElementById('myDIV'); if (x.style.display === 'none') < x.style.display = 'block'; > else < x.style.display = 'none'; > >
!DOCTYPE html> html> head> style> #myDIV !-- w w w . d e m o 2 s. c o m --> width: 500px; height: 500px; background-color: lightblue; >  body> p>Click the "Test" button to toggle between hiding and showing the DIV element: button onclick="myFunction()">Test div id="myDIV"> This is my DIV element. p>b>Note: The element will not take up any space when the display property set to "none". script> function myFunction() < let x = document.getElementById('myDIV'); if (x.style.display === 'none') < x.style.display = 'block'; > else < x.style.display = 'none'; > >   

Example

Difference between «inline», «block» and «none»:

let whichSelected = x.selectedIndex; let sel = x.options[whichSelected].text; let elem = document.getElementById("mySpan"); elem.style.display = sel;
!DOCTYPE html> html> body> p>Select a display type in the list below to change span id="mySpan" style="color:blue;">this span element's  display type. select onchange="myFunction(this);" size="3"> option>block option>inline option>none !-- w w w . d e m o 2 s . c o m --> script> function myFunction(x) < let whichSelected = x.selectedIndex; let sel = x.options[whichSelected].text; let elem = document.getElementById("mySpan"); elem.style.display = sel; >   

Example

Return the display type of a

element:

console.log(document.getElementById("myP").style.display);
!DOCTYPE html> html> body> p id="myP" style="display:none;">This is a p element. button type="button" onclick="myFunction()"> Return the display type of p  p id='demo'>  script> function myFunction() < document.getElementById('demo').innerHTML = document.getElementById("myP").style.display; >   

  • Javascript DOM Style cssFloat Property
  • Javascript DOM Style cursor Property
  • Javascript DOM Style direction Property
  • Javascript DOM Style display Property
  • Javascript DOM Style emptyCells Property
  • Javascript DOM Style filter Property
  • Javascript DOM Style flex Property

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Style display Property

The display property sets or returns the element’s display type.

Elements in HTML are mostly «inline» or «block» elements: An inline element has floating content on its left and right side. A block element fills the entire line, and nothing can be displayed on its left or right side.

The display property also allows the author to show or hide an element. It is similar to the visibility property. However, if you set display:none , it hides the entire element, while visibility:hidden means that the contents of the element will be invisible, but the element stays in its original position and size.

Tip: If an element is a block element, its display type can also be changed with the float property.

Browser Support

Syntax

Return the display property:

Property Values

Value Description
block Element is rendered as a block-level element
compact Element is rendered as a block-level or inline element. Depends on context
flex Element is rendered as a block-level flex box. New in CSS3
inline Element is rendered as an inline element. This is default
inline-block Element is rendered as a block box inside an inline box
inline-flex Element is rendered as a inline-level flex box. New in CSS3
inline-table Element is rendered as an inline table (like ), with no line break before or after the table
list-item Element is rendered as a list
marker This value sets content before or after a box to be a marker (used with :before and :after pseudo-elements. Otherwise this value is identical to «inline»)
none Element will not be displayed
run-in Element is rendered as block-level or inline element. Depends on context
table Element is rendered as a block table (like ), with a line break before and after the table)
table-caption Element is rendered as a table caption (like )
table-cell Element is rendered as a table cell (like and )
table-column Element is rendered as a column of cells (like )
table-column-group Element is rendered as a group of one or more columns (like )
table-footer-group Element is rendered as a table footer row (like )
table-header-group Element is rendered as a table header row (like )
table-row Element is rendered as a table row (like
table-row-group Element is rendered as a group of one or more rows (like )
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

Technical Details

Default Value: inline
Return Value: A String, representing the display type of an element
CSS Version CSS1

More Examples

Example

Difference between the display property and the visibility property:

function demoDisplay() <
document.getElementById(«myP1»).style.display = «none»;
>

function demoVisibility() document.getElementById(«myP2»).style.visibility = «hidden»;
>

Example

Toggle between hiding and showing an element:

function myFunction() <
var x = document.getElementById(‘myDIV’);
if (x.style.display === ‘none’) <
x.style.display = ‘block’;
> else <
x.style.display = ‘none’;
>
>

Example

Difference between «inline», «block» and «none»:

function myFunction(x) <
var whichSelected = x.selectedIndex;
var sel = x.options[whichSelected].text;
var elem = document.getElementById(«mySpan»);
elem.style.display = sel;
>

Example

Return the display type of a

element:

Источник

Скрыть / показать элементы JavaScript

Скрыть / показать элементы JavaScript

  1. Используйте свойство style.visibility , чтобы скрыть / показать элементы HTML
  2. Используйте свойство style.display , чтобы скрыть / показать элементы HTML
  3. Используйте jQuery hide() / show() , чтобы скрыть / показать элементы HTML
  4. Используйте jQuery toggle() , чтобы скрыть / показать элементы HTML
  5. Используйте addClass() / removeClass() , чтобы скрыть / показать элементы HTML

Мы часто сталкиваемся с ситуациями, когда мы хотим переключиться между отображением и скрытием элемента. В этом руководстве рассказывается, как скрыть / отобразить элемент в JavaScript.

Используйте свойство style.visibility , чтобы скрыть / показать элементы HTML

Свойство style.visibility , когда установлено значение hidden, делает целевой элемент скрытым, но не удаляет его из потока. Итак, целевой элемент отображается, но не отображается. Это не влияет на планировку и позволяет другим элементам занимать свое естественное пространство. Мы можем снова сделать целевой элемент видимым, вернув для свойства значение visible .

document.getElementById(id).style.visibility = "visible"; // show  document.getElementById(id).style.visibility = "hidden"; // hide 

Используйте свойство style.display , чтобы скрыть / показать элементы HTML

Свойство style.display , когда установлено в none , удаляет целевой элемент из обычного потока страницы и позволяет остальным элементам занимать его пространство. Хотя целевой элемент не отображается на странице, мы все равно можем взаимодействовать с ним через DOM. Затрагиваются все потомки, и они не отображаются так же, как родительский элемент. Мы можем снова сделать целевой элемент видимым, установив для свойства значение block . Желательно установить display как » , потому что block добавляет поле к элементу.

document.getElementById(id).style.display = 'none'; // hide  document.getElementById(id).style.display = ''; // show 

Используйте jQuery hide() / show() , чтобы скрыть / показать элементы HTML

  1. Скорость : определяет скорость задержки эффекта затухания.
  2. Замедление : определяет функцию замедления, используемую для перехода в видимое / скрытое состояние. Принимает два разных значения: свинг и линейный .
  3. Обратный вызов : это функция, выполняемая после завершения выполнения метода show() .

Точно так же метод jQuery hide() помогает скрыть выбранные элементы. Принимает те же 3 параметра, что и show() .

$("#element").hide(); // hide  $("#element").show(); // show 

Источник

Читайте также:  Animate css при скроллинге
Оцените статью