Home > Web Front-end > JS Tutorial > Javascript tips to generate html elements_javascript tips

Javascript tips to generate html elements_javascript tips

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 16:48:05
Original
1087 people have browsed it

There are two methods for Javascript to generate HTML elements. This article introduces two methods. The first is a more formal method of creating elements, and the second is to use the write() method in JavaScript to directly write HTML elements.

Method 1:

Copy code The code is as follows:

//createElement() creates the input element into the obj object
var obj = document.createElement('input');
//Select the previous element to generate the location
var before = document.getElementById('before');
//Set obj Element name, value
obj.name='name';
obj.value='value';
//setAttribute can freely define attributes, not only id and type, name and value can also be This definition
obj.setAttribute('id','idname');
obj.setAttribute('type','typename');
//Add obj
after the previous element before .appendChild(obj);

Method 2:

Copy code The code is as follows:

document.write("");

Here are two methods of generating HTML elements. The first one is more standardized and has more code, while the second one has less code.

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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template