批改状态:合格
老师批语:你是刚报名的, 还是之前一直没写作业, 得抓紧时间了
<a href="www.php.cn" target="_self"></a>
<a href="www.php.cn" target="_blank"></a>
<a href="www.php.cn" target="_parent"></a>
<a href="www.php.cn" target="_top"></a>
<a href="a.png" download="img下载.png"></a>
<a href="tel:18345678901">18345678901</a>
<a href="tomail:demo@163.com">demo@163.com</a>
<a href="#bottom">到达底部</a><p>文本</p>.......<p id="bottom">底部</p>
<ul><li>首页</li><li>php工具</li><li>视频课程</li></ul>
<ol><li>html/css</li><li>javascript</li><li>php</li></ol>
<dl><dt>前端</dt><dd>html</dd><dd>css</dd><dd>js</dd><dt>服务端</dt><dd>php</dd><dd>java</dd><dd>c</dd><dt>数据库</dt><dd>memcacheed</dd><dd>redis</dd><dd>mysql</dd></dl>
p{position:relative;top:50 px;left:30px;}
p{position:absolute;top:50 px;left:30px;}
<button onclick="console.log(this.innerText)">按钮</button>
注:当用对象属性方式对同一个元素绑定了对个相同事件时,只有最后的事件有效
<button>按钮</button><script>document.querySelectorAll('button')[0].onclick=function(){console.log(this.innerText);}</script>
注:两次事件都会触发
<button>按钮</button><script>const btn=document.querySelectorAll('button')[0];btn.addEventListener('click',function(){console.log('第一次点击');})btn.addEventListener('click',function(){console.log('第二次点击');})</script>
<div><li><a href="">点击</a></li></div><script>const a = document.querySelector("a");const li = document.querySelector("li");const div = document.querySelector("div");const body = document.body;a.addEventListener("click", showTagName);li.addEventListener("click", showTagName);div.addEventListener("click", showTagName);body.addEventListener("click", showTagName);function showTagName() {alert(this.tagName);}</script>
<div><li><a href="">点击</a></li></div><script>const a = document.querySelector("a");const li = document.querySelector("li");const div = document.querySelector("div");const body = document.body;a.addEventListener("click", showTagName, true);li.addEventListener("click", showTagName, true);div.addEventListener("click", showTagName, true);body.addEventListener("click", showTagName, true);function showTagName() {alert(this.tagName);}</script>
IE浏览器
e.stopPropagation();
e.cancleBubble=true
<ul><li>item1</li><li>item2</li><li>item3</li><li>item4</li><li>item5</li></ul><script>document.querySelector("ul").addEventListener("click", function (ev) {console.log(ev.target); //事件触发者console.log(ev.currentTarget); //事件绑定者});</script>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title><style>* {margin: 0;padding: 0;box-sizing: border-box;}a {color: #bbb;text-decoration: none;}#nav {background-color: black;height: 50px;line-height: 50px;}li {list-style: none;margin: 0 10px;float: left;}#nav > li > a:hover {color: white;}#nav > li {position: relative;}#nav > li > ul {position: absolute;top: 50px;width: 180px;border: 1px solid #aaa;border-top: none;}#nav > li > ul > li a {display: inline-block;height: 50px;color: #444;}ul.sub li:hover {background-color: #eee;}#nav > li > ul {/* display: none; */}</style></head><body><ul id="nav"><li><a href="">首页</a></li><li><a href="">视频教程</a></li><li><a href="">入门教程</a></li><li><a href="">社区问答</a></li><li><a href="">技术文章</a><ul><li><a href="">PHP教程</a></li><li><a href="">PHP框架</a></li><li><a href="">html教程</a></li><li><a href="">css教程</a></li><li><a href="">js教程</a></li><li><a href="">mysql教程</a></li></ul></li><li><a href="">资源下载</a><ul><li><a href="">PHP工具</a></li><li><a href="">在线工具</a></li><li><a href="">手册下载</a></li><li><a href="">学习课件</a></li></ul></li><li><a href="">PHP培训</a></li></ul></body><script>const navs = document.querySelectorAll("#nav > li");navs.forEach(function (nav) {nav.addEventListener("mousemove", showSubMenu);nav.addEventListener("mouseout", closeSubMenu);});function showSubMenu(ev) {if (ev.target.nextElementSibling !== null) {ev.target.nextElementSibling.style.display = "block";}}function closeSubMenu(ev) {if (ev.target.nodeName === "A" && ev.target.nextElementSibling !== null) {ev.target.nextElementSibling.style.display = "none";}}</script></html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号