Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<button onclick="setBg(this)">按钮2</button>
<script>
function setBg(ele){
document.body.style.backgroundColor='wheat';
ele.style.backgroundColor="yellow";
ele.textContent="保存成功";
}
</script>
event.type
event.currentTarget
event.target
event.stopPropagation();
event.preventDefault();
//案例
function show(){
console.log(1);
}
btn2.addEventListener("click",show,false);
先添加一个事件
let i = 0;
btn3.addEventListener("click",()=>{
console.log(i);
i += 1;
},false);
const myclick = new Event('click');
//派发是事件(调用一次btn3的myclick)
btn3.dispatchEvent(myclick);
//案例
<button data-index="1" data-work-unit="php" onclick="getIndex(this)">btn1</button>
function getIndex(btn){
//获取自定义属性值
console.log(btn.dataset.index);
console.log(btn.dataset.workUnit);
}
//案例
const items = document.querySelectorAll("ul > li");
for(let i = 0;i<items.length;i++){
items[i].style.color = "red";
}
<form id="login" >
<input type="text" name="email" >
</form>
//login是form.id
//email是input name值
//获取email input 元素
document.forms.login.email
四个插入位置
afterBegin :结束标签之前
beforeBegin :开始标签之前
afterEnd :结束标签之后
beforeEnd :接收标签之前
//留言板案例
<!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>
<input type="text" placeholder="留言内容" autofocus onkeydown="addMsg(this)" >
<ul class="list" ></ul>
<script>
function addMsg(ele){
if (event.keuy === 'Enter') {
//1.获取留言列表的容器
const ul = document.querySelector('.list');
//2.判断留言是否为空
if(ele.value.length === 0){
alert('留言内容不能为空');
ele.focus();
return false;
}
//3.添加一条新留言
const li = document.createElement('li')
li.textContent = ele.value;
//添加删除留言功能
li.textHTML = ele.value + "<button onclick='del()' >删除</button>"
//插入
// ul.append(li);
ul.insertAdjacentElement('afterBegin',li);
//4.清空留言的输入框,为下一次做准备
ele.value = null;
//5.重置焦点到留言框中,方便用户更次输入
ele.focus();
}
}
function del(ele){
//删除前确认
if(confirm("是否删除?")){
//方案1
ele.remove();
//方案2
// ele.outerHTML = null;
}
}
</script>
</body>
</html>
setTimeout(()=>{
},2000);
setInterval(()=>{
},2000);