文章 评论 浏览 30
贴个完整的
<html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body> <input type="text" /> <script> function throttle(fn, delay) { let timer; let isFirstCall = true; return function (...args) { const ctx = this; if (isFirstCall) { isFirstCall = false; fn.apply(ctx, args); return; } if (timer) return; timer = setTimeout(() => { timer = null; fn.apply(ctx, args); }, delay); }; } function input(e) { if (e.target.composing) { return; } console.log(e.target.value); } function onCompositionStart(e) { e.target.composing = true; } function onCompositionEnd(e) { const event = new CustomEvent("input"); e.target.composing = false; e.target.dispatchEvent(event); } function onCompositionUpdate(e) { console.log(e.target.value); } const inputEle = document.getElementById("spell_input"); inputEle.addEventListener("input", throttle(input, 1000)); inputEle.addEventListener("compositionstart", onCompositionStart); inputEle.addEventListener("compositionend", onCompositionEnd); inputEle.addEventListener("compositionupdate", onCompositionUpdate); </script> </body> </html>
文章 0 评论 0
接受
贴个完整的
第 79 题:input 搜索如何防抖,如何处理中文输入