Javascript tips to generate html elements_javascript tips
WBOY
Release: 2016-05-16 16:48:05
Original
1053 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.
//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);
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