js 模拟用户输入到输入框里

function userinput(text) {
  const input = document.getElementById('chat-input');//element
  input.value = text;
  input.dispatchEvent(new Event('input', { bubbles: true }));
  input.dispatchEvent(new Event('change', { bubbles: true }));
  input.focus();
  input.setSelectionRange(input.value.length, input.value.length);
  document.execCommand("InsertText", false, text);
}

 

posted @ 2024-07-23 09:26  herry菌  阅读(21)  评论(0编辑  收藏  举报