1. CoreThe browser web page is a Dom tree structure
To operate a 1. Get the Dom node<body> <div id="div1"> <h1 id="h"></h1> <p class="p1"></p> </div> </body> //By tag document.getElementsByTagName('h1'); //By id document.getElementById('div1'); //Through class document.getElementsByClassName('p1'); //Get the parent node h.parentElement //Get all child nodes under the parent node div1.children[index]; //Get the first node div1.firstElementChild under the current node; //Get the last node div1.lastElementChild under the current node; //Get the next node h.nextElementSibling; 2. Update Node//Update the value of the text div1.innerText='Modify the value of the text'; //Update hypertext, you can parse html text tags div1.innerHTML='<strong>Add hypertext</storng>'; //Update css div1.style.color='red'; div1.style.fontSize='20px';//CamelCase naming div1.style.padding='2em'; 2.1 Practical ExerciseGet id Before the change Operation Input 3. Delete Dom nodeSteps to delete a node: first get the parent node, then delete yourself through the parent node let self=document.getElementById('p1');//Get the deleted node let father=self.parentElement//Get the parent node father.removeChild(self);//Delete the node through the parent node
4. Insert node We get a Dom node. Assuming that this Dom node is empty, we can add an element through <body> <p id="p1">Java</p> <div id="div1"> <p>JavaScript</p> <p>css</p> <p>C</p> </div> </body> 4.1 Insert existing tagslet a=document.getElementById('div1'); let b = document.getElementById('p1'); a.appendChild(b);//append Effect 4.2 Create a new tag to insertlet a=document.createElement('p');//Create node p tag a.id='p2'; a.innerText='hello, Xiao Shen'; let list = document.getElementById('div1'); //Get div1 id list.appendChild(a); //Append child nodes //Create label nodes let a=document.createElement('script'); a.setAttribute('type','text/javascript'); Effect: //Create style tag node let st=document.createElement('style'); st.setAttribute('type','text/css'); st.innerHTML='body{ background-color:pink;}'; let hd = document.getElementsByTagName('head')[0]; //Note that head is the 0th element. hd.appendChild(st); Effect: 4.3 Insert before child node (insertBefore)<html> <head> <meta charset="utf-8"> <title></title> </head> <body> <p id="p1">Java</p> <div id="div1"> JavaScript <p id='p2'>css</p> <p id="p3">C</p> </div> <script type="text/javascript"> //The parent node with child nodes let list = document.getElementById('div1'); let self = document.getElementById('p2'); let before=document.getElementById('p1'); list.insertBefore(before,self); </script> </body> </html> Effect: This is the end of this article about JavaScript Dom object operations. For more relevant JavaScript Dom object operations, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Usage instructions for the docker create command
>>: Summary of principles for writing HTML pages for emails
In a front-end technology group before, a group m...
Table of contents 1. Environmental Preparation 2....
Gird layout has some similarities with Flex layou...
MySQL can be connected not only through the netwo...
There are some problems with the compressed versi...
I recently encountered a problem. The emoticons o...
When encapsulating Vue components, I will still u...
Preface MySQL query uses the select command, and ...
Table of contents Million-level data processing s...
This article example shares the specific code of ...
This article shares the specific code for JavaScr...
1. Create a user: Order: CREATE USER 'usernam...
1. Basic structure: Copy code The code is as follo...
Share a beautiful input box animation style libra...
No gaps on both sides, gaps between each column w...