html之事件传播机制和定时器

事件传播机制:当元素嵌套且绑定相同类型事件时,会自动触发其父辈绑定的事件

阻止事件传播

html 复制代码
//阻止事件传播
var box = document.querySelector(".box")  
box.onclick=function(e){}
e.stopPropagation()

监听事件

html 复制代码
//添加事件监听器
//box.addEventListener(事件类型,事件函数,事件传播阶段) 
var child = document.querySelector(".child")  //孩子
var parent = document.querySelector(".parent") //父亲
var grandpa = document.querySelector(".grandpa")  //爷爷
child.addEventListener("onclick",function(){},false)     //默认false,为冒泡阶段;true,为捕获阶段
//加上之后则执行到child就不会自动执行父亲和爷爷的点击事件

周期定时器和延迟定时器

setInterval(function,time):周期定时器 function指执行函数,time指时间间隔,单位是毫秒

clearInterval(定时器标志变量) :定时器停止

setTimeout(function,time):延迟定时器 function指执行函数,time指时间间隔,单位是毫秒

clearTimeout(定时器标志变量) :定时器停止

html 复制代码
<!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>
        #aa{
            width: 130px;
            height: 130px;
            background: linear-gradient(0deg, red, yellow, blue);
        }
    </style>
</head>
<body>
    <div id="aa"></div>
    <!-- <input type="range" id="change" min="0" value="50" max="360"> -->
    <input type="button" value="停止" id="stop">
    <input type="button" value="开启" id="start">
    <script>
        // setInterval(function,time):周期定时器   function指执行函数,time指时间间隔,单位是毫秒
        // setTimeout(function,time):延迟定时器   function指执行函数,time指时间间隔,单位是毫秒
        // var c_btn = document.getElementById("change")
        var c_div = document.getElementById("aa")
        var stop = document.getElementById("stop")
        var start = document.getElementById("start")
        // c_btn.oninput=function(){
        //     var current_value = this.value
        //     c_div.style.background="linear-gradient("+current_value+"deg, pink, yellow, blue)"
        // }
        
        var current_value = 0
        var once = 1
        var demo = function(){
            c_div.style.background="linear-gradient("+(current_value++)+"deg, red, yellow, blue)"
            test = setTimeout(demo,100)
        }
        
        // var test = setInterval(demo,100)
        var test = setTimeout(demo,100)
        stop.onclick=function(){
            // clearInterval(test)
            clearTimeout(test)
            once = 0
        }
        start.onclick=function(){
            if(once==1){
                return
            }
            // test = setInterval(demo,100)
            test = setTimeout(demo,100)
            once=1
        }
    </script>
</body>
</html>
相关推荐
艾莉丝努力练剑2 分钟前
文件描述符fd:跨进程共享机制
java·linux·运维·服务器·开发语言·c++
进击的尘埃5 分钟前
Vue 3 编译器宏的编译时魔法:defineModel、defineSlots 与 AST 转换的真相
javascript
不会敲代码17 分钟前
使用 Mock.js 模拟 API 数据,实现前后端并行开发
前端·javascript
向上的车轮14 分钟前
TypeScript 一日速通指南:数据类型全解析与转换指南
javascript·typescript
Java面试题总结14 分钟前
2026最新Java八股文(完整版)
java·开发语言·jvm·数据库·java面试·java八股文
6+h15 分钟前
【java】System类详解
java·开发语言·python
2501_9110882325 分钟前
C++中的代理模式变体
开发语言·c++·算法
2401_9001515430 分钟前
代码覆盖率工具实战
开发语言·c++·算法
叫我一声阿雷吧32 分钟前
【JS 实战案例】用 JS 实现页面滚动到指定位置(带动画)
javascript·页面交互·js实战案例·平滑滚动·前端零基础·锚点导航
bu_shuo33 分钟前
在命令行中编译cpp文件
开发语言·c++·cpp