JavaScript tips for generating HTML elements _javascript tips

Source: Internet
Author: User

The way Javascript generates HTML elements is described in this article in 2, the first is a more formal method of creating elements, and the second is to use the Write () method in JavaScript to write HTML elements directly.

Method One:

Copy Code code as follows:

createelement () create INPUT element into obj object
var obj = document.createelement (' input ');
Select the previous element where you want to build the site
var before = document.getElementById (' before ');
Set obj element name, value
Obj.name= ' name ';
Obj.value= ' value ';
SetAttribute are free to define attributes, not only the IDs and Type,name and value can also be defined in this
Obj.setattribute (' id ', ' idname ');
Obj.setattribute (' type ', ' TypeName ');
Add obj after the previous element
Before.appendchild (obj);

Method Two:

Copy Code code as follows:

document.write ("<inpt name= ' name ' value= ' value ' id= ' idname ' type= ' TypeName ' >");

Here is a list of these 2 ways to generate HTML <input> elements, the first comparison specification, the code is more, the second relatively little code.

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.