[Related recommendations: javascript video tutorial, web front-end]
// 1 Get element node // by id (find elements by id, Case sensitive, if there are multiple ids, only the first one will be found) document.getElementById('p1');
// Search for elements by class name. Separate multiple class names with spaces to get an HTMLCollection (a collection of elements with a length attribute, and you can access an element inside through the index number) var cls = document.getElementsByClassName('a b'); console.log(cls);
// Search through the name attribute and return a NodeList (a node collection with a length attribute that can be accessed through the index number) var nm = document.getElementsByName('c'); console.log(nm);
// Find elements by tag name and return an HTMLCollection document.getElementsByTagName('p');
The document.querySelector('.animated')
document.querySelector('.animated')
in the Document Object Model (DOM) ), each node is an object. DOM nodes have three important attributes
: 1. nodeName: the name of the node
2. nodeValue: the value of the node
3. nodeType: the type of the node
1. nodeName attribute: the name of the node, which is read-only.
2. nodeValue Attribute: The value of the node.
3. nodeType attribute: the type of the node, which is read-only. The following commonly used node types:
1. Create a node: createElement('')
// Create an element. It is just created and not added to the html. It needs to be used in conjunction with appendChild. var elem = document.createElement('p'); elem.id = 'test'; elem.style = 'color: red'; elem.innerHTML = 'I am a newly created node'; document.body.appendChild(elem);
2. Insert node: appendChild ()
var oNewp=document.createElement("p"); var oText=document.createTextNode("World Hello"); oNewp.appendChild(oText);
2-1. Insert node: insertBefore()
var oOldp=document.body.getElementsByTagName("p")[0]; document.body.insertBefore(oNewp,oOldp);
1. Delete node: removeChild
var op=document.body.getElementsByTagName("p ")[0]; op.parentNode.removeChild(op);
1. Clone node: parent.cloneNode() false or true
// Clone node (needs to accept a parameter to indicate whether to copythe element)
element) var form = document.getElementById('test'); var clone = form.cloneNode(true); clone.id = 'test2'; document.body.appendChild(clone);
1. Replace node method node.replace(new,old)
var oOldp=document.body.getElementsByTagName("p")[0]; oOldp.parentNode.replaceChild(oNewp,oOldp);Function
(function() { var start = Date.now(); var str = '', li; var ul = document.getElementById('ul'); var fragment = document.createDocumentFragment(); for(var i=0; i<10000; i++) { li = document.createElement('li'); li.textContent = ''+i+'th child node'; fragment.appendChild(li); } ul.appendChild(fragment); console.log('Time consuming:'+(Date.now()-start)+'milliseconds'); // 63 milliseconds})();
[Related recommendations: javascript video tutorials, web front-end]
The above are JavaScript knowledge points Organize the details of obtaining elements and nodes. For more information, please pay attention to other related articles on the source code network!