43 lines
1.4 KiB
HTML
43 lines
1.4 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Document</title>
|
|
|
|
</head>
|
|
<body>
|
|
<p id="greet1" class="greet">hello world</p>
|
|
<p id="greet2" class="greet">hello world
|
|
<span id="yu">asd</span>
|
|
|
|
</p>
|
|
<div id="draw"></div>
|
|
<script>
|
|
// // 获取元素
|
|
// var greet= document.getElementById("greet1") //id
|
|
// var greet1=document.getElementsByClassName("greet")
|
|
// // var gre=document.getElementsByName("xxx")
|
|
// // var gre1=document.getElementsByTagName 标签
|
|
// // 除了id之外的获取方式 其他的获取结果都是数组 id的获取方式是单个元素
|
|
|
|
// console.log(greet1)
|
|
// // div.parentsibling
|
|
//创建节点
|
|
var squeare=document.createElement("div");
|
|
squeare.innerHTML="我是div"
|
|
squeare.style.color="red"
|
|
squeare.setAttribute("name","mydiv") //setattribute 设置属性 第一个参数是属性名 第二个参数是属性值
|
|
var draw=document.getElementById("draw")
|
|
draw.append(squeare)
|
|
|
|
console.log(squeare)
|
|
var greet2=document.getElementById("greet2")
|
|
var span=document.getElementById("yu")
|
|
greet2.remove(yu)
|
|
// greet2.removeChild(yu) //删除子元素
|
|
|
|
</script>
|
|
</body>
|
|
</html> |