从目前找到的资料来看,这个模拟不能起到所有的作用,主要体现在键盘的有一些事件不能完整的体现 出来
先做个记录吧。
//这个就是用来输入字科符的
var inpEle=document.getElementById("stockCode");
var st ='000001'
var evt = new InputEvent('input', {
inputType: 'insertText',
data: st,
dataTransfer: null,
isComposing: false
});
inpEle.value = st;
inpEle.dispatchEvent(evt);
//敲回车
let keyCoke = 13
let keyboardEvent = document.createEvent('KeyboardEvent')
let initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? 'initKeyboardEvent' : 'initKeyEvent'
keyboardEvent[initMethod]('keyup', true, true, window, false, false, false, false, keyCoke, 0)
document.getElementById('stockCode').dispatchEvent(keyboardEvent)
//不知道为什么弄的那个网站还得这样处理一下
function fireKeyEvent(el, evtType, keyCode) {
var evtObj;
if (document.createEvent) {
evtObj = document.createEvent('UIEvents');
evtObj.initUIEvent(evtType, true, true, window, 1);
delete evtObj.keyCode;
if (typeof evtObj.keyCode === "undefined") {//为了模拟keycode
Object.defineProperty(evtObj, "keyCode", { value: keyCode });
} else {
evtObj.key = String.fromCharCode(keyCode);
}
if (typeof evtObj.ctrlKey === 'undefined') {//为了模拟ctrl键
Object.defineProperty(evtObj, "ctrlKey", { value: true });
} else {
evtObj.ctrlKey = true;
}
el.dispatchEvent(evtObj);
}
}
fireKeyEvent(document.getElementById('stockCode'),'keyup',13)