console.log(document.documentElement)
console.log(document.head)
console.log(document.body)
<body>
<div id="box"></div>
<script>
var box = document.getElementById('box')
console.log(box) //
</script>
</body>
<body>
<div class="box"></div>
<script>
var box = document.getElementByClassName('box')
console.log(box) //
</script>
</body>
// item [0].innerHTML = "news-1"
for (var i=0;i<items.length;i++)
item[i].innerHTML = "news-"+i
}
//Set →Array.from将伪数组转化为真数组
var newitems = Array.from(items)
console,log(newitems.filter)
<body>
<div></div>
<script>
var box = document.getElementByTagName("div")
console.log(box) //
//得到的也是一个伪数组
</script>
</body>
<body>
<input name="username" type="test"/>
<script>
var box = document.getElementByName("username")
console.log(box[0]) //
</script>
</body>
#
查找.
查找var item = document.querySelector("ul li")
#
查找.
查找var item = document.querySelectorAll("ul li")
<input type="text" id = "username">
<input type ="checkbox" checked id = "rember">
username.type = "password"//可以使输入框变成密码框
rember.checked= false//可以使复选框不被选择
<div id ="asd" shuxin="" ></div>
asd.setAttribute("shuxin","123456")
console.log(asd.getAttribute("shuxin"))
asd.removeAttribute("shuxin")
data-
data-
<input type ="password" id ="password">
<button id ="eyebtn">eye</button>
<script>
var passInput = document.getElementById("password")
var eyebtn = document.querySelector("#eyebtn")
eyebtn.onclick=function(){
console.log(passInput.type)
if(passInput==="password")
passInput.type="text"
}else{
passInput.type="password"
}
}
</script>
<input type="checkbox" id ="all">全选/全不选
<hr>
<ul>
<li>
<input type="checkbox">1
<li>
<li>
<input type="checkbox">2
<li>
<li>
<input type="checkbox">3
<li>
<li>
<input type="checkbox">4
<li>
</ul>
<script>
var oAll = document.querySelector("#all")
var oitems=documnet.querySelectorAll(".shop input")
//全选按钮的设置
oAll.onclick=function(){
for(var i=0;i<oitems.length;i++){
oitem[i].checked=oAll.checked
}
}
//单选按钮的设置
for(var i=0;i<oitems.length;i++){
oitems[i].onclick=handler
}
function handler(){
var count =0
//for判断每一项如果是checked acount++
for(var i=0;i<oitems.length;i++){
if(oitems[i].check) conut++
}
if(count==oitem.length){
oAll.checked=true
}else{
oAll.check=false
}
}
</script>
<div id="box">
111
</div>
box.innerHTML="222
"//会发生解析
<div id="box">
111
</div>
box.innerText="222
"//不会发生解析
<select name ="" id="select">
<option value="1">111</option>
<option value="2">222</option>
<option value="3">333</option>
<select>
select.vable="3"//将下拉框里的项目默认选为第三个
<ul>
<ul>
var filmlist = {
{
url:,
title:,
grade:
}
{
url:,
title:,
grade:
}
{
url:,
title:,
grade:
}
}
var filmItems=filmList.map(function(item){
return `<li>
<img src="${item.url}" alt="">
<h3>${item.title}</h3>
<p>观众评分${item.grade}</p>
</li>
})
console.log(filmItems.join(""))
var oul=document.querySelector("ul")
oul.innerHTML=filmItems.join("")
style
[ ]
的方法来表示<div id = "box" style="width:100px ;color:black;">111</div>
console.log(box.style.width)
console.log(box.style.["background-color"])
//也可以设置
box.style.width="200px"
getComputedStyle
获取
[ ]
的方法来表示<div id = "box" style="width:100px ;color:black;">111</div>
var obox = document.getElementById("box")
var res = getComputedStyle(obox)//获取box的全部样式
.className
<div id="box" class="item item1 item2"></div>
box.className="item"//此时item1,item2就被去除了
box.className +=" item3"//此时加进去了一个item3
classList
box.classList.add("item2")//覆盖掉原先的
box.classList.remove("item2")//删除
toggle
*{
padding=0;
margin=0
}
ul{
display:flex;
list-style:none;
}
li{
height:50px;
line-height:50px;
text-align:center;
flex:1;
}
.active{
color:red;
border-bottom;1px solid red;
}
<body>
<ul>
<li class="active" id="item1">
正在热映
</li>
<li id="item2">
即将播放
</li>
</ul>
<sricpt>
item1.onlick = function(){
item1.classList.add("active")
item2.classList.remove("active")
}
item2.onlick = function(){
item1.classList.remove("active")
item2.classList.add("active")
}
</script>
</body>
<head>
<style>
*{
margin:0;
padding:0;
}
ul{
list-style: none;
}
.header{
dispLay: flex;
width:500px;
}
.header li {
flex:1;
height: 50px;
line-height: 50px;
text-align: center;
border:1px solid black;
}
.box {
position: relative;
}
.box li {
position: absolute;
left:0;
top:0;
width: 500px;
height: 200px;
background-color:yellow;
display:none;
}
.header .active{
background-color: red;
}
.box .active{
dispLay: block;
}
</style>
</head>
<body>
<ul class=" header">
<li class="active">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<ul class="box">
<li class="active">111</li>
<li>222</li>
<li>333</li>
<li>444</li>
</ul>
<script>
var oHeaderItems = document.querySelectorAll(".header li")
var oBoxItems = document.querySelectorAl1( ".box li")
for(var i=0; i<oHeaderItems.length; i++){
oHeaderItems[i].dataset.index = i
oHeaderItems[i].onclick = handler
}
function handler(){
// console.log(this.dataset.index)
var index = this.dataset.index
for(var m=0;m<oHeaderitems.length; m++){
oHeaderItems[m].classList.remove("active")
oBoxItems[m].classList.remove( "active")
}
oHeaderItems[index].classList.add( "active")
oBoxItems [index].classList.add ( "active")
}
</script>
</body>
getElementBy...
获得到的节点getAttibute
innerText
获取到的就是元素的文本节点属性 | 作用 |
---|---|
childNodes | 获取全部节点 |
children | 获取全部元素节点 |
firstChild | 获取第一个节点 |
firstElementChild | 获取第一个元素节点 |
lastChild | 获取最后一个节点 |
lastElementChild | 获取最后一个元素节点 |
previousSibing | 找上一哥哥节点 |
previousElementSibing | 找上一个哥哥元素节点 |
nextSibing | 找上一弟弟节点 |
nextElementSibing | 找上一个弟弟元素节点 |
parentNode | 找上父节点 |
parentElement | 找上父元素节点 |
//创建一个div元素节点
var oDiv = document.createElement(' div ')
console.log(oDiv) //
父元素ID.
appendChild插入一个节点box.appendChild(odiv)
//将创建的节点加入到ID叫做box的盒子里面
父元素ID.
insertBefore(要插入的节点,谁的前面)box.insertBefore(odiv,child)
父元素ID.
removeChild(要删除的对象)
父元素ID.
replaceChild(新的节点,老的节点)
父元素ID.
cloneNode()<body id="list">
<ul>
</ul>
<script>
var arr =["111","222","333"]
for(var i=0;i<arr.length;i++){
var oli = document.createElement("li")
oli.innerHTML = arr[i]
var obutton = document.createElement( "button")
obutton.innerHTML = "delete"
obutton.onclick = handler
oli.appendchild(obutton)
list.appendChild(oli)
}
function handler(){
console.log(this.parentNode)
this.parentNode.remove()
}
</script>
</body>
<body>
<ul test="我是u1 的一个属性"">
<1i>he1lo</1i>
</u1>
<script>
//先获取u1
var oul = document. queryselector('u1 ')
//获取到ul下的第一个子元素节点,是一个元素节点
var eleNode = oul.firstElementchi1d
//获取到ul 的属性节点组合,因为是个组合,我们要拿到节点的话要用索引
var attrNode = oul.attributes[0]
//获取到u1下的第一个子节点,是一个文本节点
var textNode = oul.firstchi1d
</script>
</body>
注:
注:
var arr1 =[{
name:"哆啦A梦"
url:""
},
{
name:"哆啦A梦"
url:""
},
{
name:"哆啦A梦"
url:""
},
{
name:"哆啦A梦"
url:""
},
]
var arr2=[{
name:"哆啦A梦"
url:""
},
{
name:"哆啦A梦"
url:""
},
{
name:"哆啦A梦"
url:""
},
{
name:"哆啦A梦"
url:""
},
]
renderHTML(arr1)
function renderHTML(arr) {
for(var i=0;i<arr.length;i++){
var oli = document.createElement("li")
oli.innerHTML = `${arr[i].url}" alt="">
${arr[i].name}`
list.appendchild(oli)
}
}
isLoading = false
window.onscroll = function(){
// console.log( "1111")
var listHeight = list.offsetHeight
var listTop = list.offsetTop
// console.log( listHeight+listTop)
var scrollTop = document.documentElement.scrollTop ||document.body.scrollTop
var windowHeight = document.documentElement.clientHeight
if(isLoading) return
if((listHeight+listTop)-Math.round(windowHeight+scrollTop)<50){
console. log("到底了")
isLoading = true
//渲染下一页数据
setTimeout(function(){
renderHTML(arr2)
isLoading = false //下一次到底事件继续触发
},1000)
}
}
var oDiv = document.queryselector('div ')
oDiv.onc1ick = function(){}
//谁来触发事件=>oDiv =>这个事件的事件源就是oDiv
//触发什么事件=> onc1ick =>这个事件类型就是click
//触发之后做什么=> function (){} =>这个事件的处理函数
var oDiv = document. queryselector( ' div ')
oDiv.onclick = function () {
console.log('你点击了div')}
-事件解绑-dom2
function handler(){
console.log("谢谢惠顾")
this.removeEventListener("click",handler)
}
btn.addEventListener("click",handler)
//此时可以解绑
//有一定兼容性问题
function handler(){
console.log("谢谢惠顾")
节点.detachEvent("onclick",handler)
}
btn.addEventListener("onclick",handler)
常见的事件
window.event
,就是对事件信息的所有描述
oDiv.onclick = function(){
console.1og(window.event.X轴坐标点信息)
console.1og(window.event.Y轴坐标点信息)
}
<style>
*{
margin:0;
padding:0;
}
box{
width: 200px;
height: 50px;
background:yellow;
position: relative;
margin : 100px;
}
#box p{
width: 300px;
height: 200px;
background-color: red;
position: absolute;Left: 100px;
top:100px;
dispLay: none;
pointer-events: none;
/*穿透*/
z-index: 100;
}
</style>
<body>
<div id="box">
kerwin的头像
<p>
kerwin的介绍
</p>
</div>
<script>
box.onmouseover = function(){
this.firstElementChild.style.display="block"
}
box.onmouseout = function(){
this.firstElementChild.style.display="none"
}
box.onmousemove = function(evt){
// console.log(evt.offset×,evt.offsetY)
this.firstElementchild.style.left = evt.offsetX + "px"
this.firstElementChild.style.top = evt.offsetY + "px"
}
</script>
</body>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 100px;
height: 100px;
background-color: aqua;
position: absolute;
}
</style>
</head>
<body>
<div id="box">
</div>
<script>
box.onmousedown = function(){
console.log("down")
document.onmousemove = function(evt){
var x =evt.clientX - box.offsetWidth/2
var y = evt.clientY- box.offsetHeight/2
if(y<=0) y=0
if(x<=0) x=0
if(x>= document.documentElement.clientWidth-box.offsetWidth)
x = document.documentElement.clientWidth-box.offsetWidth
if(y>= document.documentElement.clientWidth-box.offsetHeight)
y = document.documentElement.clientWidth-box.offsetHeight
box.style.left = x + "px"
box.style.top = y + "px"
}
}
box.onmouseup = function(){
console.log("up")
document.onmousemove = null
}
</script>
</body>
节点.
stopPropagation()节点.
cancelBubble=true 来阻止dom0
return false
dom2
节点.
preventDefault()节点.
returnValue=false<style>
*{
margin:o;
padding:0;
}
ul{
list-styLe: none;
width: 200px;
padding: 10px;
border:1px solid black;
display: none;
position:absolute;
}
ul li:hover{
background:skyblue;
}
</style>
<body>
<ul>
<li>111</li>
<li>222</li>
<li>333</li>
<li>444</li>
</ul>
<script>
document.addEventListener( "contextmenu",function(evt){
evt.preventDefault()
list.style.display = "block"
var x = evt.clientx
var y = evt.clientY
if(x >= document.documentElement.clientwidth-list.offsetwidth)
x = document.documentElement.clientwidth-list.offsetwidth
if(y>=document.documentElement.clientHeight-list.offsetHeight)
y = document.documentElement.clientHeight-list.offsetHeight
list.style.left = x +“px"
list.style.top = y + “px"})
document.addEventListener( "click",()=>{
list.style.display="none"
})
</script>
</body>
事件触发
<body>
<u1>
<li>1</1i>
<1i>2</1i>
<1i>3</1i>
</u1>
<script>
var oul = docuemnt. queryselector( ' u1 ')
ou1 .addEventListener('click', function (e) {console.log('我是u1 的点击事件,我被触发了')})
</script>
</body>