DOM Document Object Model in JS

DOM (Document Object Model) refers to the Document Object Model
Primary values are used to manipulate html documents
All nodes have a parent node, and only one node has no parent element (root node) HTML
A node can have more than one child node, but a node has and only one parent node
DOM methods for JavaScript
The method is what we can do on a node (an HTML element).
Programming interfaces can access HTML DOM through JavaScript (and other programming languages).All HTML elements are defined as objects, while programming interfaces are object methods and object properties.
The method is an action you can perform (such as adding or modifying elements).
Properties are values that you can get or set, such as the name or content of a node.

HTML DOM Object-Method and Properties
1. Common DOM operation methods:
getElementById() returns an element with the specified ID.Element of ID.
getElementsByTagName() returns a list of nodes (collections/arrays of nodes) containing all elements with the specified tag name.
getElementsByClassName() returns a list of nodes that contain all the elements with the specified class name.
getElementsByName() returns a list of nodes with all elements of the specified name.
appendChild() adds a new child node to the specified node.
removeChild() deletes child nodes.
replaceChild() replaces the child node.
insertBefore() inserts a new child node in front of the specified child node.
<script type="text/javascript">
				//appendChind adds a new node to the specified node
				var aa=document.createElement('a');
				//removeChild() deletes child nodes
				var span2=document.getElementById('span2')
				//ReplaceChild replaces child nodes
				var pp=document.createElement('p');
				var span3=document.getElementById('span3')
				//InsertBefore (new node, old node) insert new node
				var span=document.createElement('span');
				var span4=document.getElementById('span4');
		<div id="div1">1
			<span id="span1">
		<div id="div2">2
			<span id="span2">
		<div id="div3">3
			<span id="span3">
		<div id="div4">
			<span id="span4">
createElement() creates element nodes.
createTextNode() creates a text node.
createAttribute() creates an attribute node.
getAttribute() returns the specified property value.
setAttribute() sets or modifies the specified property to the specified value.
<script type="text/javascript">
				//createElement() creatin
				var span=document.createElement('span');
				//	createTextNode() Creates a text node
				var tex=document.createTextNode('This is created by a text node');
				//createAttribute() Creates the attribute node
				var nod=document.createAttribute('class');
				document.getElementById('div2').setAttributeNode(nod)//<div id="div2" class="div02">2</div>
				//getAttribute() Gets the specified attribute value
				var div3=document.getElementById('div3');
				//setAttribute() Sets or modifies the value of a specified property
				//Set up:
				document.getElementById('div4').setAttribute('class','div04')//<div id="div4" class="div04">4</div>
				document.getElementById('div5').setAttribute('id','div05')//<div id="div05"></div>
		<div id="div1">1</div>
		<div id="div2">2</div>
		<div id="div3">3</div>
		<div id="div4">4</div>
		<div id="div5"></div>
2. Common attributes:
innerHTML - Text value of a node (element) 
parentNode - The parent node of a node (element)
 childNodes - Child node of a node (element)
 attributes - Attribute node of a node (element) 
nodeName - Name of the node 
nodeValue - Value of the node
 nodeType - Type of node
<script type="text/javascript">
			//innHTML sets or gets the start and end HTML tags for an element
			//Set up:
			document.getElementById('div1').innerHTML='abcdefg'//<div id="div1">abcdefg</div>
			//parentNode returns the parent node of the specified node
			console.log(document.getElementById('span3').parentNode.nodeType)//1 represents the basic label
			//nextSibling Brothers Node
			console.log(div1.nextSibling.nextSibling);//<div id="div2">...<.div>Returns the div2 collection
			//childNodes returns child nodes
			var chil=document.getElementById('div3').childNodes;
			var chill=document.getElementById('div3').children;
			console.log(chill)//HTMLCollection [span#span3, span3: span#span3]
			//Attributes returns the specified set of node attributes
			//Node Name Specifies the node name of the node
			//If the node is an element node, nodeName returns the label name
			//If the node is a property node, nodeName returns the name of the property
			//Node Value specifies the node value of the node
			//nodeType Specifies the type of node
		<div id="div1">123456</div>
		<div id="div2">aaa
			<span id="span2">
		<div id="div3">bbb
			<span id="span3">
		<div id="div4">
			<input type="text" name="inpu1" id="inpu1" value="This is a text box" />


Keywords: Javascript Attribute Programming

Added by incarnate on Sat, 03 Aug 2019 06:04:38 +0300