Custom event in HTML, Javascript

后端 未结 2 1619
自闭症患者
自闭症患者 2020-12-19 04:57

I\'m looking to create a custom event in HTML, JS.


How can I create one such? \'

2条回答
  •  [愿得一人]
    2020-12-19 05:13

    You want a CustomEvent

    They should be easy to work with but browser support is poor. However the DOM-shim should fix that.

    var ev = new CustomEvent("someString");
    var el = document.getElementById("someElement");
    el.addEventListener("someString", function (ev) {
        // should work
    });
    el.dispatchEvent(ev);
    

提交回复
热议问题