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

Invalid solution to style.display='' in js_javascript skills

WBOY
Release: 2016-05-16 16:32:42
Original
1997 people have browsed it

The example in this article describes the solution to the invalid style.display="" in js. Share it with everyone for your reference. The specific solutions are as follows:

1. Problem description:

In js, we sometimes want to dynamically control the display or hiding of a div or more operations, but if we style.display="", it may have no effect.

Look at the following piece of code:

Copy code The code is as follows:




My name is smile.



<script><br> window.onload=function(){<br> document.getElementById('name').style.display="";<br> alert("test");<br> }<br> </script>

css defines the div with the id as name as hidden, and we use js to control the id to be displayed after the page is loaded. Is this wrong?
Nothing wrong? But why is the interface still blank?

2. Solution:

Remember there is another usage: xxx.style.display="block" Let’s try it,
Woohoo, it actually showed up! ! !

Let’s take a look at the differences between the usage of style.display="" and style.display="block".
In fact, the biggest difference between the two is that block is displayed in blocks, so it will wrap. So since there is only such a difference, why can one be displayed and the other cannot be displayed in this example? It is troublesome.
Okay, let’s put this problem aside for now. Let’s first look at how we can solve this problem. In addition to using style.display="block" to solve it, there is another way:

Copy code The code is as follows:

<script><br> Document.getElementById('name').style.display="";<br> </script>

Just use style to build the style with id name into the tag, so that it can be displayed normally regardless of using display="" or display="block"!

I hope this article will be helpful to everyone’s web programming based on JavaScript.

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