<body><divclass="father"><divclass="son">点我</div></div><script>const father = document.querySelector('.father')const son = document.querySelector('.son')document.addEventListener('click',function(){alert('I am grandfather')},true)father.addEventListener('click',function(){alert('I am father')},true)son.addEventListener('click',function(){alert('I am son')},true)</script></body>
当鼠标点击"点我"时候,弹出框弹出的先后顺序是:I am grandfatherI am fatherI am son
冒泡
<body><divclass="father"><divclass="son">点我</div></div><script>const father = document.querySelector('.father')const son = document.querySelector('.son')document.addEventListener('click',function(){alert('I am grandfather')},false)father.addEventListener('click',function(){alert('I am father')},false)son.addEventListener('click',function(){alert('I am son')},false)</script></body>
当鼠标点击"点我"时候,弹出框弹出的先后顺序是:I am sonI am fatherI am grandfather
<body><divclass="father"><divclass="son">点我</div></div><script>const father = document.querySelector('.father')const son = document.querySelector('.son')document.addEventListener('click',function(){alert('I am grandfather')},false)father.addEventListener('click',function(){alert('I am father')},false)son.addEventListener('click',function(event){alert('I am son')// 阻止事件冒泡event.stopPropagation()},false)</script></body>
<body><ul><li>li-1</li><li>li-2</li><li>li-3</li><li>li-4</li><li>li-5</li><p>我是p标签</p></ul><script>// 需求:点击小li,当前li文字变红色,点击其他标签不变色const ul = document.querySelector('ul')ul.addEventListener('click',function(event){if(event.target.tagName ==='LI'){event.target.style.color ='red'// 找到点击所触发的元素:event.target}})</script></body>
阻止默认行为
<body><formaction="https://www.baidu.com"><inputtype="submit"value="免费注册"></form><script>const form = document.querySelector('form')form.addEventListener('submit',function(event){// 阻止默认行为,此处的默认行为是"提交"event.preventDefault()})</script></body>
<body><a href="">点击跳转</a><script>const a = document.querySelector('a')a.addEventListener('click', function (event) {// 阻止默认行为,此处的默认行为是"跳转"event.preventDefault()})</script>
</body>
其他事件
页面加载事件
基本认识
作用:可以控制:先加载外部资源(如:图片、CSS、JS等)加载完毕时触发的事件意义:1.有些时候,我们需要等页面所有资源全部加载完了,再做一些事情2.一些老项目的代码,往往把 script 写在 head 中
<body><divstyle="display: inline-block;">随着内容的变化,span标签的宽度也随之变化</div><script>const div = document.querySelector('div')console.log(div.clientWidth);</script></body>
一、预期效果
Element - The worlds most popular Vue UI framework
element默认样式 目标样式 二、Calendar 属性
参数说明类型可选值默认值value / v-model绑定值Date/string/number——range时间范围,包括开始时间与结束时间。开始时间必须是周一,…