• Javascript自定义事件


    触发用户自定义事件,已成为各前端框架的基本技术。(较新版本的浏览器才支持)

    页面 

    <div id="outer">
        <div id="inner">Leonardo Da Vinci</div>
    </div>
    <button onclick="tiggerMyEvent();">触发事件</button>

    脚本

    document.querySelector("#outer").addEventListener('Leonardo Da Vinci',function(e){
        console.log('捕获事件',e.detail,e);
    },false);
    
    function tiggerMyEvent(){
        var evt = (void 0);
        try{ // Chrome浏览器、Firefox浏览器
            evt = new CustomEvent('Leonardo Da Vinci',{
                detail:{ // optional and defaulting to null, of type any, that is an event-dependent value associated with the event
                    film:'忍者神龟'
                },
                bubbles:true, // (Optional) A Boolean indicating whether the event bubbles. The default is false.
                cancelable:true // (Optional) A Boolean indicating whether the event can be canceled. The default is false.
            });
        }catch(e){ // IE Edge浏览器
            evt = document.createEvent('Event'); // Event type字符串只能是事件模块中定义的值。
            evt.initEvent('Leonardo Da Vinci',true,true); // the type of event,bubbles,cancelable
            evt.detail = {
                film:'忍者神龟'
            };
        }
        document.querySelector('#inner').dispatchEvent(evt);
    }
  • 相关阅读:
    CABasicAnimation 使用
    CABasicAnimation(CAKeyframeAnimation)keypath 取值
    c++的应用领域
    QT 状态机详解 statemachine (转)
    C++默认实参
    String隐式共享
    可重入函数与不可重入函数
    堆和栈的区别(转过无数次的文章)
    Qt Model/View(转)
    C++虚函数和纯虚函数
  • 原文地址:https://www.cnblogs.com/sea-breeze/p/7242260.html
Copyright © 2020-2023  润新知