任务是将文本插入输入。但是,我无法做到这一点。尝试了以下方法:
#1(触发任何事件时重置文本,例如鼠标单击表单)
inputField.value = 'TEXT';
#2(类似于 #1)
inputField.value = 'TEXT'
inputField.dispatchEvent(new Event('input', { bubbles: true }));
#3(焦点发生但文本插入没有)
inputField.dispatchEvent(new Event('focus', { bubbles: true, cancelable: true }));
const pasteEvent = Object.assign(new Event('paste', { bubbles: true, cancelable: true }), {
clipboardData: {
getData: (type = pasteType) => 'PASTED TEXT',
},
});
inputField.dispatchEvent(pasteEvent);
输入:
<input data-marker="code-input" name="code" maxlength="20" autocomplete="off" placeholder="Ввод" type="text" class="input-input-Zpzc1" value="">
1 个回答