一个前端,爱跑步、爱吉他、爱做饭、爱生活、爱编程、爱南芳姑娘,爱我所爱。世间最温暖又无价的是阳光、空气与爱,愿它们能带你去更远的地方。

  • 文章
  • 心情
  • 照片墙
  • 留言板
  • 工具
  • 友链
  • biaoblog

    专注web开发技术分享

    自定义事件的触发 dispatchEvent 的用法

    技术 206 2023-03-21 12:50

    页面A:

     // 创建自定义派发时间,并调用
     let setEvent = new Event('setItemEvent')
        setEvent.newValue = '1'
        window.dispatchEvent(setEvent)
    


    B页面:

    //根据自己需要来监听对应的key
        //根据自己需要来监听对应的key
        window.addEventListener("setItemEvent", function(e) {
    
          //e.newValue : 是可以对应的新值
          if (e.newValue === "1") {
            console.log(e.newValue);
          }
        });
    

    文章评论

    评论列表(0