How to insert an element to the front of the body element in js

Source: Internet
Author: User

After opening a homepage today, an animation is displayed in the header. It was easy to add the expanded code. It can be found that the header file of the original program is stored in head.html and then included. In this example, we need to put a div for expansion in head.html. However, all the pages have the expansion effect, but I only need to have this effect on the homepage. There are two solutions:

1. Set the display of the div in the header to none, and call js to set it to block.

2. Use js to dynamically create a div and place the div at the beginning. This adds a few more words, as shown below:

<Script language = "javascript" type = "text/javascript"> var divObj = document. createElement ("div"); divObj. innerHTML = "hi, welcome to php programmer tutorial network! "; Var first = document. body. firstChild; // get the first element document. body. insertBefore (divObj, first); // Insert before the first element </script>

Next we will share with you how to dynamically create html elements (using createElement) in FireFox)

Let's take a look at the following code (this code is problematic ):

<script type="text/javascript">function add(){var input=document.createElement("<input size=30 type=\"text\" name=\"extrachildren[]\">")var br=document.createElement("<br>");var br2=document.createElement("<br>");document.getElementById("children").insertBefore(br);document.getElementById("children").insertBefore(br2);document.getElementById("children").insertBefore(input);</script>

The above code is feasible in IE, but it does not work in FireFox. To play a role in FireFox, you must replace it with the following code:

<script type="text/javascript">function add(){    var input=document.createElement('input');    input.setAttribute('size','30');    input.setAttribute('type','text');    input.setAttribute('name','extrachildren[]');    var parent=document.getElementById('children');    parent.insertBefore(document.createElement('br'),null);    parent.insertBefore(document.createElement('br'),null);    parent.insertBefore(input,null);}</script>

You can create an element in either of the following ways:

Document. createElement ("<input type = text> ")
Document. createElement ("<input> ")
Document. createElement ("input ")

Firefox only supports one method:

Document. createElement ("input ");
Document. setAttribute (name, value );

Articles you may be interested in
  • Insert element in js to the front of the page body
  • Jquery obtains the index number of the current element in the Peer element.
  • Note the following when querying strings with single quotes and inserting strings with single quotes in Mysql:
  • How does php Delete the first and last elements of an array?
  • A classic dialogue between programmers and testers. These are shared by foreign programmers, who say they are used globally?
  • Code used to test the password strength of a user input using Javascript
  • Js restrictions: Only English letters and numbers can be entered. Chinese characters and other special characters cannot be entered.
  • JQuery determines whether an element exists

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: 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.