简体   繁体   English

过滤contenteditable / input中的输入以将插入符号放在原处之后

[英]after filtering input in contenteditable/input to put caret where it was

I have a contenteditable element what I update/filter with javascript. 我有一个contenteditable元素,该用JavaScript更新/过滤什么。 After updating the textcontent the but the caret always goes back to the beginning. 在更新textcontent之后,但插入符号总是返回到开头。 A want to make to work like the input element. 一个想要像输入元素一样工作的人。 I set up a demo : 我建立了一个演示

  1. test1 - contenteditable element test1-contenteditable元素
  2. test2 - input element test2-输入元素

In this example I have a function what enable to insert only numbers, if you insert other characters nothing will show. 在此示例中,我具有一个仅允许插入数字的功能,如果您插入其他字符,则不会显示任何内容。

After entering some numbers to the contenteditable and inserting a NOT number, then the cursor (caret) goes to the start. 在内容编辑器中输入一些数字并插入一个非数字后,光标(尖号)将开始。 In case of input element the cursor goes to the end. 如果是输入元素,光标将移至末尾。

How to achieve that, to remain the cursor to the position where it was? 如何做到这一点,将光标保持在原位置? (pure javascript) (纯JavaScript)

 function onlyNumber(element) { const invalidChars = /\\D/g; ob = element.target; if (element.target.nodeName == "INPUT") { if (invalidChars.test(ob.value)) { ob.value = ob.value.replace(invalidChars, ""); } } else if (element.target.nodeName == "TD") { if (invalidChars.test(ob.textContent)) { ob.textContent = ob.textContent.replace(invalidChars, ""); } } } document.getElementById("test1").addEventListener("input", function(event) { onlyNumber(event); }) document.getElementById("test2").addEventListener("input", function(event) { onlyNumber(event); }) 
 #test1 { border: 1px solid gray; padding: 5px; width: 100px; } #test2 { margin-top: 15px; } 
 <table class="table"> <tbody> <tr> <td id="test1" contenteditable="true"></td> </tr> </tbody> </table> <input id="test2" /> 

You could use the keydown event, and prevent all but numbers and arrows/delete/backspace. 您可以使用keydown事件,并阻止除数字和箭头/删除/退格键以外的所有内容。

Stack snippet 堆栈片段

 function onlyNumber(element) { const invalidChars = /\\D/g; ob = element.target; if (element.target.nodeName == "INPUT") { if (invalidChars.test(ob.value)) { ob.value = ob.value.replace(invalidChars, ""); } } else if (element.target.nodeName == "TD") { if (invalidChars.test(ob.textContent)) { ob.textContent = ob.textContent.replace(invalidChars, ""); } } } document.getElementById("test1").addEventListener("keydown", function(event) { if ((event.keyCode < 58 && event.keyCode > 47) || (event.keyCode < 41 && event.keyCode > 36) || event.keyCode == 8 || event.keyCode == 46) { return true; } event.preventDefault(); }) document.getElementById("test2").addEventListener("input", function(event) { onlyNumber(event); }) 
 #test1 { border: 1px solid gray; padding: 5px; width: 100px; } #test2 { margin-top: 15px; } 
 <table class="table"> <tbody> <tr> <td id="test1" contenteditable="true"></td> </tr> </tbody> </table> <input id="test2" /> 


Another option would be to re-position the cursor: 另一个选择是重新定位光标:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM