<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js手动触发事件</title>
<script>
window.onload = function(){
var oA = document.getElementById('clickme');
var DownloadEvt = null;
if (document.createEvent) {
if (DownloadEvt === null){
DownloadEvt = document.createEvent('MouseEvents');
}
DownloadEvt.initEvent('click', true, false);
oA.dispatchEvent(DownloadEvt);
} else if (document.createEventObject) {
oA.fireEvent('onclick');
} else if (typeof oA.onclick == 'function') {
oA.onclick();
}
}
</script>
</head>
<body>
<a id='clickme' href='https://blog.csdn.net/qq_36412715/article/details/104636155'></a>
</body>
</html>
JS手动触发事件用到的方法:
参数:eventType 共5种类型:
Events :包括所有的事件.
HTMLEvents:包括 'abort', 'blur', 'change', 'error', 'focus', 'load', 'reset', 'resize', 'scroll', 'select',
'submit', 'unload'. 事件
UIEvents :包括 'DOMActivate', 'DOMFocusIn', 'DOMFocusOut', 'keydown', 'keypress', 'keyup'.
间接包含 MouseEvents.
MouseEvents:包括 'click', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup'.
MutationEvents:包括 'DOMAttrModified', 'DOMNodeInserted', 'DOMNodeRemoved',
'DOMCharacterDataModified', 'DOMNodeInsertedIntoDocument',
'DOMNodeRemovedFromDocument', 'DOMSubtreeModified'.
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 )
dom.dispatchEvent(eventObject), 参数eventObject表示事件对象,是createEvent()方法返回的创建的Event对象。
需要注意的是在IE 8以下版本请用fireEvent方法。
参考:https://www.cnblogs.com/jiangxiaobo/p/5830200.html