【Web】前端主动触发事件

creatEvent —> initEvent —> dispatchEvent

if(el.dispatchEvent){
     
    var evt = document.createEvent('Event');
    evt.initEvent(type,true,true);
    el.dispatchEvent(evt);
}else if(el.fireEvent){
     
    el.fireEvent('on'+type);
}
  1. createEvent(eventType)
    参数:eventType :

    • Events :

      包括所有的事件.

    • HTMLEvents:

      包括 ‘abort’, ‘blur’, ‘change’, ‘error’, focus’, ‘load’, ‘reset’, ‘resize’, ‘scroll’, ‘select’,‘submit’, ‘unload’.

    • UIEevents :

      包括 ‘DOMActivate’, ‘DOMFocusIn’, ‘DOMFocusOut’, ‘keydown’, ‘keypress’, ‘keyup’.

      间接包含 MouseEvents.

    • MouseEvents:

      包括 ‘click’, ‘mousedown’, ‘mousemove’, ‘mouseout’, ‘mouseover’, ‘mouseup’.

    • MutationEvents:

      包括 ‘DOMAttrModified’, ‘DOMNodeInserted’, ‘DOMNodeRemoved’, ‘DOMCharacterDataModified’, ‘DOMNodeInsertedIntoDocument’, ‘DOMNodeRemovedFromDocument’, ‘DOMSubtreeModified’.

  2. initEvent(type, true, true)

    • HTMLEvents 和 通用 Events:
      initEvent( ‘type’, bubbles, cancelable )
    • UIEvents :
      initUIEvent( ‘type’, bubbles, cancelable, windowObject, detail )
    • MouseEvents:
      initMouseEvent( ‘type’, bubbles, cancelable, windowObject, detail, screenX, screenY, clientX, clientY, ctrlKey, altKey, shiftKey, metaKey, button, relatedTarget )
    • MutationEvents :
      initMutationEvent( ‘type’, bubbles, cancelable, relatedNode, prevValue, newValue, attrName, attrChange )

你可能感兴趣的:(Web,问题或技巧)