事件监听
1、获取元素
2、添加事件监听
<style>
div {
width: 300px;
height: 300px;
background-color: pink;
}
.yincang {
display: none;
}
</style>
</head>
<body>
<button>切换</button>
<div class="yincang"></div>
<script>
// 1、获取元素
const btn = document.querySelector('button')
const box = document.querySelector('div')
// 2、添加事件监听
btn.addEventListener('mouseenter', () => {
// console.log('1111')
box.classList.toggle('yincang')----切换类名
}, true)
</script>
</body>
事件类型
鼠标点击 click
鼠标经过 mouseenter
鼠标离开 mouseleave
获得焦点 focus
失去焦点 blur
键盘按下事件 keydown
用户输入事件 input
事件绑定
1、获取元素
元素.on事件名 = 回调函数
<body>
<button>点击</button>
<script>
// 1、获取元素
let btn = document.querySelector('button')
// e.on事件名 = 回调函数
btn.onclick = function () {
// console.log('111')
btn.style.backgroundColor = 'pink'
}
</script>
</body>
常见事件
<body>
<!-- -->
<script>
document.addEventListener('keyup', function (e) {
// console.log(e)
// console.log('1111')
if (e.key === 'Enter') {
console.log('我要发布微博了')
}
})
</script>
</body>
事件委托
儿子事件委托给父亲
=>箭头函数没有this
<body>
<ul>
<li>1111</li>
<li>2222</li>
<li>3333</li>
<li>4444</li>
</ul>
<script>
let li1 = document.querySelector('ul li:nth-child(1)')
li1.addEventListener('click', function () {
this.style.backgroundColor = 'pink'
})
</script>
</body>
<body>
<ul>
<li>1111</li>
<li>2222</li>
<li>3333</li>
<li>4444</li>
</ul>
<script>
let ul = document.querySelector('ul')
ul.addEventListener('click', function (key) {
console.log(key)
key.target.style.backgroundColor = 'pink'
})
</script>
</body>
捕获、冒泡
<style>
.grandfather {
width: 1200px;
height: 1200px;
background-color: aqua;
}
.father {
width: 800px;
height: 800px;
background-color: pink;
}
.son {
width: 300px;
height: 300px;
background-color: blueviolet;
}
</style>
</head>
<body>
<div class="grandfather">
<div class="father">
<div class="son"></div>
</div>
</div>
<script>
document.querySelector('.grandfather').addEventListener('click', (e) => {
console.log('我是yeye')
e.stopPropagation()
}, true)
// 默认为false冒泡 true捕获
document.querySelector('.father').addEventListener('click', (e) => {
console.log('我是父亲')
e.stopPropagation()
}, true)
document.querySelector('.son').addEventListener('click', () => {
console.log('我是儿子')
}, true)
</script>
</body>
阻止默认行为
默认行为
<form action="#">
<input type="text" name="uname" id="">
<button>tijaio</button>
阻止默认行为e.preventDefault
<body>
<form action="#">
<input type="text" name="uname" id="">
<button>tijaio</button>
</form>
<script>
document.querySelector('button').addEventListener('click', (e) => {
e.preventDefault()
})
</script>
</body>
lode事件
在页面或某个资源加载成功时触发
外面所有东西加载完成后再执行里面的
<script>
window.addEventListener('load', function () {
document.querySelector('button').addEventListener('click', () => {
console.log('1111')
})
})
</script>
</head>
<body>
<button>点击</button>
</body>
节点操作
查找结点
查找父节点
console.log(son.parentNode)
查找子节点
console.log(ul.children)
创建节点
document.createElement(‘标签名’)
删除节点
父元素.removeChild(要删除的元素)
<body>
<div class="father">
我是父亲
<div class="son1">我是儿子</div>
<div class="son">我是儿子</div>
<div class="son">我是儿子</div>
<div class="son">我是儿子</div>
<div class="son">我是儿子</div>
</div>
<script>
let father = document.querySelector('.son').parentNode
console.log(father)
let son = document.querySelector('.father').children
console.log(son)
document.querySelector('.son1').nextElementSibling.style.color = 'pink'
</script>
</body>
追加节点
删除节点
父元素.removeChild(要删除的元素)
<style>
li {
list-style: none;
width: 500px;
height: 100px;
background-color: pink;
margin-bottom: 10px;
}
</style>
</head>
<body>
<button>
发布
</button>
<ul>
<li>11111</li>
<li>2222</li>
<li class="three">3333</li>
<li>4444</li>
</ul>
<script>
const btn = document.querySelector('button')
const ul = document.querySelector('ul')
btn.addEventListener('click', () => {
// 1、创建节点
let newLi = document.createElement('li')
//2、添加内容
newLi.innerHTML = `<h1>我是新创建的li</h1> <button>删除</button>`
// 3、追加
let p2 = newLi.querySelector('button')
console.log(p2)
p2.addEventListener('click', () => {
ul.removeChild(newLi)----删除节点
})
ul.append(newLi)
})
</script>
</body>
滚动事件
<style>
body {
height: 4000px;
background-color: pink;
}
</style>
</head>
<body>
<script>
</script>
</body>
构造函数
<body>
<script>
// let obj = {
// uname: 'zs',
// age: 13
// }
// let obj1 = {
// uname: 'wdd',
// age: 15
// }
// let obj3=new Object()
function Obj(uname, age, gender) {
this.uname = uname,
this.age = age,
this.gender = gender
}
// 原型 将不变的方法,直接定义在prototype上,让所有对象的实例共享
Obj.prototype.sing = function () {
console.log('爱编程')
}
// 构造函数的原型上存在构造器(constructor属性)指向构造函数,完成双向指定
console.log(Obj.prototype)
// let obj1 = new Obj('gouxin', 23, '女')
// console.log(obj1)
// let obj2 = new Obj('jiangjia', 33, 'nan')
// obj2.age = 22
// console.log(obj2)
// obj2.sing = function () {
// console.log('no')
// }
// obj1.sing()
// obj2.sing()
// let str1=''
// let str=new String()
let obj = new Obj('gouxin', 21, 'nv')
obj.sing()
let obj2 = new Obj('jiangjia', 33, 'nan')
obj2.sing()
</script>
</body>
构造函数02
<body>
<script>
function Obj(uname, age, gender) {
this.uname = uname,
this.age = age,
this.gender = gender
}
Obj.prototype = {
//必须手动指明构造器所指向的构造函数
constructor: Obj,
sing: function () {
console.log('我要唱歌了~')
},
dance: function () {
console.log('我爱跳舞')
}
}
console.log(Obj.prototype)
let obj1 = new Obj('gouxin', 21, 'nv')
obj1.sing()
// __proto__ :对象原型,实例出来的对象中用于指向构造函数Obj的原型对象prototype的
console.log(obj1.__proto__ === Obj.prototype)
console.log(obj1.instanceof(Obj))
</script>
</body>
</html>
this关键字
<style>
div {
width: 300px;
height: 300px;
background-color: pink;
}
</style>
</head>
<body>
<button>tijiao </button>
<div></div>
<script>
// 默认指向windows ,函数基本调用时,也只想windows
console.log(this)
// 构造函数中this指向实例化的对象
// function Obj(uname, age, gender) {
// this.uname = uname,
// this.age = age,
// this.gender = gender
// console.log(this)
// }
// let obj1 = new Obj('gouxin', 21, 'nv')
// console.log(obj1)
// 对象方法中,this指向当前对象
// 箭头函数,没有this的概念,会沿着作用域链查找
let obj2 = {
uname: 'gouxin',
age: 18,
sing: () => {
console.log(this)
}
}
obj2.sing()
// function fn() {
// console.log(this)
// }
// window.fn()
// document.querySelector('button').addEventListener('click', fn)
// document.querySelector('div').addEventListener('click', fn)
// obj2.sing()
</script>
</body>
js执行机制
<body>
<script>
console.log('11')
setTimeout('console.log(\'22\')', 0)
</script>
</body>
正则表达式
<body>
<script>
let regex = /./
let str = '1222gouxingouxin'
// console.log(regex.test(str))
// .字符 除回车(\r)、换行(\n) 、行分隔符(\u2028)和段分隔符(\u2029)以外的所有字符
// console.log(str.match(/./))
// console.log(regex.exec(str))
// console.log(str.match(/^.$/))
// \d 0-9之间的数字 *数量 0-n
// console.log(str.match(/.*/g))
// + 1-n个
// console.log(str.match(/[a-z]+/g))
// console.log('13193397789jndjncd'.match(/^[0-9]{11}.+$/g))
// 2119554634@qq.com
let re = /^[0-9]{8,11}@.+$/
re.test('23244@qq.com')
console.log('554363636@qq.com'.match(re))
</script>
</body>
a.html
<body>
<button>tijaio</button>
<ul>
</ul>
<script>
const btn = document.querySelector('button');
const ul = document.querySelector('ul');
let li3 = document.querySelector('.three');
btn.addEventListener('click', () => {
// 1. 创建一个新的列表项
let newLi = document.createElement('li');
// 2. 设置新列表项的内容
newLi.innerHTML = `<h1>我是新创建的li</h1><button>删除</button>`;
// 3. 在 li3 之前追加新的列表项
ul.insertBefore(newLi, li3);
// 4. 获取新列表项内的删除按钮
let p2 = newLi.querySelector('button');
p2.addEventListener('click', () => {
// 5. 当点击删除按钮时移除列表项
ul.removeChild(newLi);
});
});
</script>
</body>