Tips for generating html elements-Javascript-js tutorial

Source: Internet
Author: User
The method for generating HTML elements in Javascript is described in this article. The first method is a more formal method for creating elements, and the second method is to use the write () method in javascript to directly write html elements. The method for generating HTML elements in Javascript is described in this article. The first method is a more formal method for creating elements, and the second method is to use the write () method in javascript to directly write html elements.

Method 1:

The Code is as follows:


// CreateElement () creates the input element to the obj object.
Var obj = document. createElement ('input ');
// Select the previous element of the location to be generated
Var before = document. getElementById ('before ');
// Set the obj element name and Value
Obj. name = 'name ';
Obj. value = 'value ';
// SetAttribute can define attributes freely, not only id and type, name and value, but also
Obj. setAttribute ('id', 'idname ');
Obj. setAttribute ('type', 'typename ');
// Add obj after the previous Element
Before. appendChild (obj );

Method 2:

The Code is as follows:


Document. write (" ");

The two types of htmlElement method. The first method is more standard, the code is more, and the second method is less 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.