Javascript tips to generate html elements_javascript tips
May 16, 2016 pm 04:48 PMThere 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:
//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:
document.write("<inpt name='name ' value='value' id='idname' type='typename'>");
Here are two methods of generating HTML <input> elements. The first one is more standardized and has more code, while the second one has less code.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to implement an online speech recognition system using WebSocket and JavaScript

Recommended: Excellent JS open source face detection and recognition project

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

How to use JavaScript and WebSocket to implement a real-time online ordering system

JavaScript and WebSocket: Building an efficient real-time weather forecasting system
