js自定義事件

var oDiv = document.getElementById("myDiv") , event;
oDiv.addEventListener("myevent",function (e) {
  console.log(e.detail);
});
if(document.implementation.hasFeature("CustomEvents" , "3.0")){
  event = document.createEvent("CustomEvent");
// 事件類型,是否冒泡,是否阻止瀏覽器的默認行爲 
  event.initCustomEvent("myevent" , true , false , {a:1,b:2});
  console.log(event);
// 觸發myevent事件
  oDiv.dispatchEvent(event);
}複製代碼
相關文章
相關標籤/搜索