Javascript Basic Tutorial: Operation of DOM Nodes
How to create a new html element
To add a new element to the HTML DOM, you must first create the element (element node) and then add Append the element to an existing element
The following example:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DOM 与 CSS</title>
</head>
<body>
<div name="dv" id="dv">
<p id="p1">php 中文网</p>
<p id="p2">php.cn</p>
</div>
<script type="text/javascript">
var para=document.createElement("p"); //创建新的<p> 元素:
var node=document.createTextNode("欢迎学习javascript");//创建了一个文本节点
para.appendChild(node);//必须向 <p> 元素追加这个文本节点
var element=document.getElementById("dv");//找到一个已有的元素
element.appendChild(para);//在已存在的元素后添加新元素
</script>
</body>
</html>How to delete a node
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DOM 与 CSS</title>
</head>
<body>
<div name="dv" id="dv">
<p id="p1">php 中文网</p>
<p id="p2">php.cn</p>
<p id="p3">js 快速入门</p>
</div>
<script type="text/javascript">
var parent=document.getElementById("dv");//找到 id="dv" 的元素:
var child=document.getElementById("p3"); //找到 id="p3" 的 <p> 元素:
parent.removeChild(child);//从父元素中删除子元素
</script>
</body>
</html>
new file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DOM 与 CSS</title>
</head>
<body>
<div name="dv" id="dv">
<p id="p1">php 中文网</p>
<p id="p2">php.cn</p>
</div>
<script type="text/javascript">
var para=document.createElement("p"); //创建新的<p> 元素:
var node=document.createTextNode("欢迎学习javascript");//创建了一个文本节点
para.appendChild(node);//必须向 <p> 元素追加这个文本节点
var element=document.getElementById("dv");//找到一个已有的元素
element.appendChild(para);//在已存在的元素后添加新元素
</script>
</body>
</html>
Preview
Clear
- Course Recommendations
- Courseware download
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning
Let's briefly talk about starting a business in PHP
Quick introduction to web front-end development
Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
Login verification and classic message board
Computer network knowledge collection
Quick Start Node.JS Full Version
The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
















