首页 > 其他分享 >事件注册与事件代理学习

事件注册与事件代理学习

时间:2024-06-13 23:55:21浏览次数:12  
标签:console log h2 bth4 代理 alert 事件 注册 click

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body style="display:grid;gap:0.5em;">
    <!-- 1.通过元素的属性:on +事件名称(事件属性),直接写到元素中的 缺点:将JS代码过多写入到html中,没有实现元素行为和结构的彻底分离-->
    <button onclick="alert('hello')">标签属性</button>
    <!-- <button onclick="hello()">提交</button> -->
    <script>
        function hello(){
            alert("hello");
        }
    </script>

    <!-- 2.元素对象添加属性的方式,动态添加到元素中缺点:事件方法之间相互覆盖 -->
    <button>动态属性</button>
    <script>
     const bth2=document.querySelector("button:nth-of-type(2)");
     bth2.onclick=function(){
     alert(1);
     };
     
      bth2.onclick=function(){
      alert(200);
     };
    //  事件的删除
     bth2.onclick=null;
    </script>
    
    <!-- 事件监听器 -->
 
    <button>监听器</button>
     <button>点击广告</button>
    <script>
        // bth3.addEventListener(事件类型,事件回调/方法名,是否冒泡/false)
        const bth3=document.querySelector("button:nth-of-type(3)");
       
        bth3.addEventListener("click",function(){
            alert('aaa');
        });
        // bth3.addEventListener("click",function(){
        //     alert('bbb');
        // });

        // 移除浏览器弹窗bbb
        // 如果事件方法使用了回调,则无法移除
        // bth3.removeEventListener("click",function(){
        //     alert("bbb");
        // });
        
        bth3.addEventListener("click",show);
        function show(){
            alert("bbb");
        }
        bth3.removeEventListener("click",show);

       
        // 事件派发
        // const myclick= new Event("click");
        // const bth4 =document.querySelector("button:nth-of-type(4)");
        // bth4.onclick=()=>alert(333); 

        // bth4.dispatchEvent(myclick);
        // let i=0;
        // bth4.addEventListener("click",()=>{
        //     console.log("点击了广告,已经赚了:" + i +"元");
        //     i += 0.2;
        // });


        const myclick= new Event("click");
        const bth4 =document.querySelector("button:nth-of-type(4)");
        // bth4.onclick=()=>alert(333); 
        // 间歇式定时器开启
      
        let i=0;
        bth4.addEventListener("click",()=>{
            console.log("点击了广告,已经赚了:" + i +"元");
            i += 0.2;
        setInterval (()=>bth4.dispatchEvent(myclick),3000);
        });
    </script>
    
</body>
</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>
        .red{
            color: red;
        }
        .bgc{
            background-color:yellow;
        }
        .blue {
          color: blue;
        }
        h2{
        border:5px solid red;
        }
        
    </style>
</head>
<body>
    <h2 style = "font-size:50px;">hello world</h2>
    
</body>
<script>
    // 过时了
    // document.querySelector("h2").className="red bgc";
    const h2 = document.querySelector("h2");
    // h2.classList:动态设置元素的class
    // 1.添加红色和背景色黄色
    h2.classList.add("red");
    h2.classList.add("bgc");
    // 2.移除背景色黄色
    h2.classList.remove("bgc");
    // 3.替换:将红色替换成蓝色
    h2.classList.replace("red","blue");

    // 添加背景色蓝色
    h2.style.backgroundColor = "green";
    // 拿到这个颜色的color值,返回是blue
    console.log(h2.className);
    // 拿到这个元素hello word的高和宽
    console.log(h2.hight);
    console.log(h2.width);
    // 因为"-"是非法标识符所以要去掉,S大写变成驼峰式
    console.log(h2.style.fontSize);
    // 计算属性
    // (获取那个元素的样式,支持伪元素)
    let styles=window.getComputedStyle(h2,null);
    console.log(styles);
    // 拿到当前元素的所有样式,不局限styles内联样式
    // 拿到高度
    console.log(styles.getPropertyValue("height"));
    console.log(styles.getPropertyValue("width"));

</script>
</html>

  

标签:console,log,h2,bth4,代理,alert,事件,注册,click
From: https://www.cnblogs.com/QWD7986/p/18246978

相关文章

  • lvgl table的使用(重绘,事件,行选中,点击,蒙版)
     ////验证//密码//人脸//刷卡#include"baseapp.h"staticlv_group_t*appGroupBtn;staticlv_obj_t*infoMeterLVGLBrushCard=NULL;staticlv_obj_t*infoTextareaMeterPasswdValue;staticlv_obj_t*appObjCamera;staticlv_obj_t*appObjCameraAiFaceImg;stat......
  • 如何搭建边缘服务器,部署 Cloudflare Worker,实现Internet网络代理。
    何为CloudflareWorker?CloudflareWorker是Cloudflare提供的一种服务,它允许开发者在全球分布的边缘服务器上运行自定义的JavaScript代码。CloudflareWorker可以用来处理HTTP请求,从而允许开发者通过编写JavaScript代码来实现各种功能,例如路由请求、修改请求和响......
  • 代理离线服务器yum、curl命令访问网络
    在CentOS7上,你可以使用Squid作为代理服务器。以下是在CentOS7上设置代理的简要步骤:1.在可以上网的服务器上安装Squid:sudoyuminstallsquid2.配置Squid:打开Squid配置文件进行编辑:sudonano/etc/squid/squid.conf找到并编辑以下行(确保取消注释并设置合适的值):http_access......
  • MySQL 新建事件执行存储过程
    开启事件调度器SETGLOBALevent_scheduler=ON;编写事件注意:命令行会将分号【;】识别为结束符,可以使用navicat创建事件;或者使用DELIMITER关键字替换分号/*如果error_event事件存在,则先删除它*/DROPEVENTIFEXISTSerror_event;/*创建一个名为error_event的事......
  • requests 开启charles代理报错
    当前报错urllib3版本:1.26.7解决代理报错的几种方式:配置代理请求proxies={'http':'http://127.0.0.1:8888','https':'http://127.0.0.1:8888'}data=requests.post(url,json=body,headers=headers,verify=False,pr......
  • jmeter做一个注册的脚本
    前置处理器:在请求之前做的操作在前置处理器里后置处理器:收到响应之后的操作在后置处理器里1、抓包获取注册接口   2、复制URL、参数等信息到jmeter  3、jmeter添加监听器-察看结果树运行脚本查看结果 啥意思没明白,反正脚本没成功,如果脚本成功响应数据应该是类......
  • 一键查询全球芯片原厂代理商信息,易采购&替代BOM功能强势登场!
    道合顺电子元器件采购神器“易采购”“替代BOM”重磅上线啦!从此用户可轻松获取芯片原厂代理商联系方式并通过替代算法让BOM一键配齐,真正实现采购/研发团队的降本增效!随着全球化经济的不断深入,企业采购面临的挑战日益复杂:信息不对称、供应商筛选耗时、价格不透明等问题限制了......
  • 禁止Windows用户使用cmd、powershell、注册表、*.bat脚本、修改网卡IP等操作
    如果你想让你的计算机或域控用户的计算机禁止使用cmd、powershell、注册表、bat脚本、修改网卡IP等操作1. 使用本地组策略(如果是本地用户)如果是非域用户,如果你在Windows10Pro或更高版本上,可以使用本地组策略(LocalGroupPolicy)来限制访问。打开“本地组策略编辑器”(gpedit.......
  • C#如何查看/写入日志到Windows事件查看器
    Windows事件日志Windows操作系统将与计算机的系统性能、应用程序和安全方面相关的每个事件记录在C:\WINDOWS\system32\winevt的日志中。事件查看器从这些原始事件日志中读取信息,然后以可读格式呈现信息。打开Windows事件查看器的方法是1、运行输入eventvwr2、Windows+X打......
  • vue代办事件案例实战练习,配有答案解析
    代办事件案例该案例,综合了前面所学的知识,列入点击事件绑定,双向绑定,v-for循环语句,v-model双向绑定,以及input标签的不同type形式。演示代码如下:<template><divid="kuagjia"><div><divclass="top"><spanstyle="margin-left:-700px;">学习计划表......