批改状态:合格
老师批语:因为字符串可以当成数组访问, 所以许多方法与数组的功能,参数几乎一致
<button onclick="console.log(this.innerHTML)">click</button>
<button>点击</button><script>const btn = document.querySelector("button:nth-of-type(2)");btn.onclick = function () {console.log(this.innerHTML);};</script>
addEventListener(事件类型,事件回调方法, 触发阶段)
<button>事件监听</button><script>const btn2 = document.querySelector("button:nth-of-type(3)");btn2.addEventListener("click", function () {console.log(this.innerHTML);});//重复定义click事件btn2.addEventListener("click", function () {console.log("重复定义了,但是没问题");});</script>

const handle = () => console.log("最后一次");btn2.addEventListener("click", handle);//移除btn2.removeEventListener("click", handle);
<button>广告收入</button><script>const btn3 = document.querySelector("button:nth-of-type(4)");//自定义事件 用evconst ev = new Event("click");let i = 0;btn3.addEventListener("click", function () {console.log("广告被点击了", "挣了", i, "元");i += 1;});//使用间歇式定时器来自动点击广告,1000=1秒setInterval("btn3.dispatchEvent(ev)", 1000);</script>

<ul class="list"><li class="item">item1</li><li class="item">item2</li><li class="item">item3</li><li class="item">item4</li></ul><script>const lis = document.querySelectorAll("li");lis.forEach((li) =>(li.click = (ev) => {// 事件绑定者// console.log(ev.currentTarget);// 事件触发者// console.log(ev.target);// 事件传递的路径// console.log(ev.path);// 阻止事件冒泡// ev.stopPropagation();}));//windowwindow.addEventListener("click", (ev) => console.log(ev.currentTarget), true);// documentdocument.addEventListener("click", (ev) => console.log(ev.currentTarget), true);// htmldocument.documentElement.addEventListener("click", (ev) => console.log(ev.currentTarget), true);// bodydocument.body.addEventListener("click", (ev) => console.log(ev.currentTarget), true);// uldocument.querySelector("ul").addEventListener("click", (ev) => console.log(ev.currentTarget), true);</script>

// uldocument.querySelector("ul").addEventListener("click",ev=>console.log(ev.currentTarget), false);// bodydocument.body.addEventListener("click",ev=>console.log(ev.currentTarget),false);// htmldocument.documentElement.addEventListener("click",ev => console.log(ev.currentTarget), false);// documentdocument.addEventListener("click",ev=>console.log(ev.currentTarget),false);// windowwindow.addEventListener("click",ev=>console.log(ev.currentTarget),false);

// 事件代理: 也叫"事件委托"const lis = document.querySelectorAll("li");// 遍历每个li,并逐个为它添加点击事件// lis.forEach(li=>(li.onclick=ev=>console.log(ev.currentTarget)));//事件代理方式document.querySelector("ul").addEventListener("click", ev => {// 事件绑定者console.log(ev.currentTarget);// 事件触发者,通常是"事件绑定者"的子元素console.log(ev.target);//查看事件触发者内容console.log(ev.target.innerHTML);});
<form action="" method="POST" name="login" id="login"><label class="title">用户登录</label><label for="username">用户名:</label><input type="text" name="username" /><label for="password">密码:</label><input type="password" name="userpwd" /><button name="submit">登录</button></form><script>const login = document.forms.namedItem("login");//去掉默认的提交动作login.submit.onclick = (ev) => {ev.preventDefault();ev.stopPropagation();//非空验证方法isEmpty(ev.currentTarget.form);};//声明非空方法函数function isEmpty(form) {if (form.username.value.length === 0) {alert("用户名不能为空");//关闭弹窗后获得焦点form.username.focus();return false;} else if (form.userpwd.value.length === 0) {alert("密码不能为空");form.username.focus();return false;} else {alert("登录成功");}}</script>
<label><input type="text" name="message" /></label><ol id="list"></ol><script>// 获取元素const msg = document.querySelector("input");const list = document.querySelector("#list");msg.onkeydown = ev => {// 键盘事件中,key表示按下的键名// console.log(ev.key);if (ev.key === "Enter") {// 非空判断if (ev.currentTarget.value.length === 0) {alert("内容不能为空");} else {// 将留言内容添加到列表中// 创建留言let str = `<li>${ev.currentTarget.value}</li>`;// 应该将最新的信息永远放在第一条list.insertAdjacentHTML("afterbegin", str);// 清空上一条留言ev.currentTarget.value = null;}}};</script>

let str = "html".concat(" css ", "php !");
str = "hello php.cn";//从起始位置开始 起始值0let res = str.slice(0, 5);console.log(res);//输出hello//负数是从结束位置开始 起始值-1res = str.slice(-6, 9);
//从0开始,取值长度5res = str.substr(0, 5);
let psw = " root888 ";console.log(psw.trim().length);
res = "admin@php.cn".split("@");console.log(res[0]);console.log(res[1]);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号