JS in the use of style.display= "" Invalid Solution

Source: Internet
Author: User

Let's look at an example first.

The code is as follows Copy Code

<style>
#name
{
Display:none;
}
</style>
<body>
<div id= "Name" >
The My name is smile.
</div>
</body>
<script>
Window.onload=function () {
document.getElementById (' name '). style.display= "";
Alert ("Test");
}
</script>

The CSS defines the ID?? Ame Div for hidden, and we in the page after the completion of the load with JS control of the ID displayed, this write wrong?
Wood wrong? But why is the interface still blank?
Do you remember? Another use is xxx.style.display= "block" then let's try it,
Alas, it shows!!!.

Let's look at the difference between style.display= "" and style.display= "block" usage.

In fact, the biggest difference between the two is that block is displayed, so it will be wrapped, so since it is so different, why in this example one can show that one does not? troubled.

Okay, let's get this over with, let's see how we can solve this problem first, besides using style.display= "block", there is another way:

The code is as follows Copy Code

<div id= "name" style= "Display:none" >
The My name is smile.
</div>
<script>
document.getElementById (' name '). style.display= "";
</script>

It is to put the style of ID name into the label, so that no matter the display= "" or display= "block" can be normal display!!!

Share your own doing, we do not put display= "", but directly set block or none so that there is no problem on the, try not to use empty or other characters oh.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.