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);
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.
Statement:
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