英文版请见
如果可以请查看英文版,其中两个回答都有些问题,可以参考。查看英文版可以运行代码段。
我有一个输入框,然后想将输入的字母五个五个分开来,但是如果我不在末尾输入或者删除的话,光标就会自动跳到末尾。
比如现在输入框里有ABCDE FGHJK
;如果我删除开头的A
的话,那么光标会自动跳到末尾。
let Input = document.createElement("input");
Input.setAttribute("type", "text");
Input.style.width = "100%";
Input.style.height = "30px";
Input.style.fontSize = "20px";
// The code AFTER this need modification and improvement
Input.addEventListener("input", function(e) {
Input.value = Input.value.replace(/\s*/g,"");
Input.value = Input.value.replace(/(.{5})/g, '$1 ');
Input.value = Input.value.trim();
});
// The code BEFORE this need modification and improvement
document.body.appendChild(Input);
如何修改代码阻止光标移到末尾,请不要使用原生js以外的如css html jquery等。
代码尽量简洁谢谢!