File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed
Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+ < head >
4+ </ head >
5+ < body >
6+ < div id ="div1 ">
7+ < p id ="p1 "> this is p1</ p >
8+ < p id ="p2 "> this is p2</ p >
9+ </ div >
10+ < div id ="div2 ">
11+ < p id ="p3 "> this is p3</ p >
12+ < p id ="p4 "> this is p4</ p >
13+ </ div >
14+ < script type ="text/javascript ">
15+ var p = document . createElement ( 'p' ) ;
16+ p . innerHTML = 'new p' ;
17+ var div1 = document . getElementById ( 'div1' ) ;
18+ adiv1 . appendChild ( p ) ;
19+ </ script >
20+ </ body >
21+ </ html >
Original file line number Diff line number Diff line change 2626* [ 04-01] ( https://github.com/TYRMars/JSlearn#04-01 ) ` 从基础知识到JS-Web-API `
2727* [ 04-02] ( https://github.com/TYRMars/JSlearn#04-02 ) ` DOM本质 `
2828* [ 04-03] ( https://github.com/TYRMars/JSlearn#04-03 ) ` DOM节点操作 `
29+ * [ 04-04] ( https://github.com/TYRMars/JSlearn#04-04 ) ` DOM结构操作 `
2930
3031
3132
@@ -1057,6 +1058,53 @@ p.getAttribute('style');
10571058p .setAttribute (' style' ,' font-size:30px;' );
10581059```
10591060
1061+ ## 04-04
1062+ ### DOM结构操作
1063+ * 新增节点
1064+ ``` JavaScript
1065+ var div1 = document .getElementById (' div1' );
1066+ // 添加新节点
1067+ var p1 = document .createElement (' p' );
1068+ p1 .innerHTML = ' this is p1' ;
1069+ div1 .appendChild (p1); // 添加新创建的元素
1070+ // 移除已有节点
1071+ var p2 = document .getElementById (' p2' );
1072+ div1 .appendChild (p2);
1073+ ```
1074+ * 获取父元素-获取子节点
1075+ ``` JavaScript
1076+ var div1 = document .getElementById (' div1' );
1077+ var parent = div1 .parentElement ;
1078+ ```
1079+ * 删除节点
1080+ ``` JavaScript
1081+ var div1 = document .getElementById (' div1' );
1082+ var child = div1 .childNodes ;
1083+ div1 .removeChild (child[0 ]);
1084+ ```
1085+ #### 举个栗子(例子:example)!!!
1086+ ``` HTML
1087+ <!DOCTYPE html>
1088+ <html >
1089+ <head >
1090+ </head >
1091+ <body >
1092+ <div id =" div1" >
1093+ <p id =" p1" >this is p1</p >
1094+ <p id =" p2" >this is p2</p >
1095+ </div >
1096+ <div id =" div2" >
1097+ <p id =" p3" >this is p3</p >
1098+ <p id =" p4" >this is p4</p >
1099+ </div >
1100+ <script type =" text/javascript" >
1101+ var p = document .createElement (' p' );
1102+ p .innerHTML = ' ' ;
1103+ </script >
1104+ </body >
1105+ </html >
1106+ ```
1107+
10601108---
10611109
10621110### JSDemo JS小程序
You can’t perform that action at this time.
0 commit comments