1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=1200, initial-scale=1.0">
<title>Document</title>
<style>
</style>
</head>
<body>
<button onclick='console.log("hello");'></button>
<script>
console.log('hello')
</script>
<script src="1.js"></script>
</body>
</html>
1.js
console.log('hello')
2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=1200, initial-scale=1.0">
<title>访问非常规元素</title>
</head>
<body>
<div id='myDiv'>test</div>
<script>
console.log(document.doctype)
console.log(typeof document.doctype)
console.log(document.documentElement)
console.log(document.head)
console.log(document.title)
console.log(document.body)
</script>
</body>
</html>
3.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=1200, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="myDiv" name="div1" class='test1'>
<p id="myPara">test</p>
</div>
<script>
const oDiv = document.getElementById('myDiv')
const oDiv1 = document.getElementsByName('div1')
const oDiv2 = document.getElementsByTagName('div')
const oDiv3 = document.getElementsByClassName('test1')
const oDiv4 = document.querySelector('#myDiv')
const oDiv5 = document.querySelectorAll('#myDiv')
console.log(oDiv,oDiv1,oDiv2,oDiv3)
console.log(oDiv4,oDiv5)
const oPara = oDiv.querySelector('#myPara')
</script>
</body>
</html>
4.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=1200, initial-scale=1.0">
<title>Document</title>
<style>
.p1 {
border: 1px solid red;
}
.p2 {
background-color:gray;
}
.p3 {
visibility:hidden;
}
</style>
</head>
<body>
<!--
// 原生属性
<div id="myDiv" class="test" style="border:1px solid red;"></div>
<img src="" alt="">
<label for="userName" >姓名:</label><input type="text" name="userName" id="">
<script>
const oDiv = document.querySelector('div')
console.log(oDiv.id)
console.log(oDiv.className)
const oLabel = document.querySelector('label')
console.log(oLabel.htmlFor)
console.log(oDiv.style)
</script> -->
<!--ClassName-->
<h1 class="p1 p2">this is a heading</h1>
<button id='myButton'>Click</button>
<script>
const heading = document.querySelector('h1')
const myButton = document.querySelector("#myButton")
//console.log(heading.className)
//heading.className += ' p3'
//console.log(heading.className)
//classList
console.log(heading.classList)
//heading.classList.add('p3')
heading.classList.remove('p2')
myButton.addEventListener('click',() =>{
heading.classList.toggle('p3')
})
</script>
<!--自定义属性-->
<div id="myDiv1" ddd="user" eee="name">test</div>
<script>
const oDiv1 = document.querySelector('#myDiv1')
let ddd = oDiv1.getAttribute('ddd')
let id = oDiv1.getAttribute('id')
console.log(ddd) //user
console.log(id) //myDiv1
oDiv1.setAttribute('eee', 'xiaojichao')
let eee = oDiv1.getAttribute('eee')
console.log(eee)
</script>
<!--HTML5 自定义数据属性-->
<div id="myDiv2" data-user-name="Tianguiping" data-title="teacher">Test2</div>
<script>
const myDiv2 = document.querySelector('#myDiv2')
console.log(myDiv2.dataset)
console.log(myDiv2.dataset.userName)
myDiv2.dataset.title = 'student'
console.log(myDiv2.dataset.title)
</script>
</body>
</html>
5.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=1200, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>this is a heading</h1>
<p>this is <i>a little</i> sample</p>
<script>
const myPara = document.querySelector('p')
// beforebegin、afterbegin、beforeend、 afterend
myPara.insertAdjacentHTML('beforebegin','<i>test</i>')
myPara.insertAdjacentHTML('beforeend','<i>test2</i>')
myPara.insertAdjacentText('afterbegin','testsdasdfasdfa;sdjf')
console.log(myPara.innerHTML)
console.log(myPara.outerHTML)
///= 只访问里面的文本
console.log(myPara.textContent)
console.log(document.documentElement.textContent)
console.log(myPara.innerText)
</script>
</body>
</html>
6.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=1200, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul>
<li>Java</li>
<li>JavaScript</li>
<li>Python</li>
<li>Swift</li>
</ul>
<script>
const oList = document.querySelector('ul')
console.log(oList.parentNode)
console.log(oList.parentElement)
// 得到 oList 元素的子节点
console.log(oList.childNodes)
console.log(oList.children)
//访问第一个元素
console.log(oList.firstChild)
console.log(oList.firstElementChild)
//访问最后一个元素
console.log(oList.lastElementChild)
// 访问相邻的元素
console.log(oList.firstElementChild.nextElementSibling)
console.log(oList.lastElementChild.previousElementSibling)
// 元素节点的数目
console.log(oList.childNodes.length)
console.log(oList.childElementCount)
</script>
</body>
</html>
7.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=1200, initial-scale=1.0">
<title>Document</title>
<style>
.p1 {
visibility: hidden;
}
</style>
</head>
<body>
<ul>
<li>Java</li>
<li>JavaScript</li>
</ul>
<input type="text" id="txtItem" />
<button id="btnAdd">+</button>
<button id="btnDel">-</button>
<script>
const oList = document.querySelector('ul')
document.querySelector('#btnAdd').addEventListener('click',() => {
let strItem = document.querySelector("#txtItem").value
let oE = document.createElement('li')
oE.textContent = strItem
oList.appendChild(oE)
document.querySelector("#txtItem").value = ''
if (oList.childElementCount > 0) {
document.querySelector('#btnDel').classList.remove('p1')
}
})
document.querySelector('#btnDel').addEventListener('click',() => {
oList.removeChild(oList.lastElementChild)
if (oList.childElementCount == 0) {
document.querySelector('#btnDel').classList.toggle('p1')
}
})
</script>
</body>
</html>
8.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=1200, initial-scale=1.0">
<title>Document</title>
<style>
.p1 {
visibility: hidden;
}
</style>
</head>
<body>
<ul>
<li>Java</li>
<li>JavaScript</li>
</ul>
<input type="text" id="txtItem" />
<button id="btnAdd">+</button>
<button id="btnDel">-</button>
<script>
const oList = document.querySelector('ul')
document.querySelector('#btnAdd').addEventListener('click',() => {
let strItem = document.querySelector("#txtItem").value
let oE = document.createElement('li')
oE.textContent = strItem
oList.appendChild(oE)
document.querySelector("#txtItem").value = ''
if (oList.childElementCount > 0) {
document.querySelector('#btnDel').classList.remove('p1')
}
})
document.querySelector('#btnDel').addEventListener('click',() => {
oList.removeChild(oList.lastElementChild)
if (oList.childElementCount == 0) {
document.querySelector('#btnDel').classList.toggle('p1')
}
})
</script>
</body>
</html>