難點(diǎn)不是插入空格,而是修正光標(biāo)的位置,這個(gè)只支持IE9+、chrome瀏覽器
注意:這個(gè)使用了jquery框架
核心代碼
$(function() { $('#kahao').on('keyup', function(e) { //只對(duì)輸入數(shù)字時(shí)進(jìn)行處理 if((e.which >= 48 && e.which <= 57) || (e.which >= 96 && e.which <= 105 )){ //獲取當(dāng)前光標(biāo)的位置 var caret = this.selectionStart //獲取當(dāng)前的value var value = this.value //從左邊沿到坐標(biāo)之間的空格數(shù) var sp = (value.slice(0, caret).match(/\s/g) || []).length //去掉所有空格 var nospace = value.replace(/\s/g, '') //重新插入空格 var curVal = this.value = nospace.replace(/(\d{4})/g, "$1 ").trim() //從左邊沿到原坐標(biāo)之間的空格數(shù) var curSp = (curVal.slice(0, caret).match(/\s/g) || []).length //修正光標(biāo)位置 this.selectionEnd = this.selectionStart = caret + curSp - sp } }) })
完整代碼:已經(jīng)測(cè)試
銀行卡號(hào)4位空格 <script> $(function() { $(&#39;#kahao&#39;).on(&#39;keyup&#39;, function(e) { //只對(duì)輸入數(shù)字時(shí)進(jìn)行處理 if((e.which >= 48 && e.which <= 57) || (e.which >= 96 && e.which <= 105 )){ //獲取當(dāng)前光標(biāo)的位置 var caret = this.selectionStart //獲取當(dāng)前的value var value = this.value //從左邊沿到坐標(biāo)之間的空格數(shù) var sp = (value.slice(0, caret).match(/\s/g) || []).length //去掉所有空格 var nospace = value.replace(/\s/g, &#39;&#39;) //重新插入空格 var curVal = this.value = nospace.replace(/(\d{4})/g, "$1 ").trim() //從左邊沿到原坐標(biāo)之間的空格數(shù) var curSp = (curVal.slice(0, caret).match(/\s/g) || []).length //修正光標(biāo)位置 this.selectionEnd = this.selectionStart = caret + curSp - sp } }) }) </script>
經(jīng)過(guò)測(cè)試確實(shí)很好用,里面用到了很多的正則
\s 匹配任何空白字符,包括空格、制表符、換頁(yè)符等等。等價(jià)于[ \f\n\r\t\v]。

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

jQuery ?? ??? ?? ??? ??: ?? ?? ??? jQuery? ? ??? ??? ?? ???? JavaScript ??????, JavaScript ?????? ????? ????? ??? ??? ?????. ? ????? jQuery? ?? ??? ??? ???? ??? ??? ??? ? ??? ???? ?? ??? ?????. jQuery ?? ?? HTML ??? jQuery ?????? ???? ???. CDN ??? ?? ????? ????? ? ????.

jQuery?? PUT ?? ??? ???? ??? ?????? jQuery?? PUT ??? ??? ??? ?? ??? ??? ??? ?? ????? ? ?? ?? ??? ?? ?? ??? ???? ???. PUT ??? ????? ??????? ??? ???? ?? ??? ?? ????? ?? ???? ?????? ? ?????. ??? jQuery?? PUT ?? ???? ???? ???? ?? ?????. ?? jQuery ????? ??? ????? ??? ?? $.ajax({u? ?? PUT ??? ?? ? ????.

??: jQuery ?: ???? ?? ?? ??? ???? ??? ?????. ? ????? ???? ??? ???? ???? ?? ??? ????. jQuery? ??? ? ???? ?? ?? ??? ??? ??? ? ?? ???? ?? ??? ???, ?? ??? ???? ??? ? ????. ??? jQuery? ???? ???? ?? ?? ???? ??? ???? ??? ???? ???? ?? ??? ?????. ?? jQuery ????? ??? ???? ?? ??? ???? ?????? ???? ???. <

??: jQuery? ???? ?? ??? ??? ??? ?????. jQuery? DOM ??? ???? ? ?? ???? ?? ?? JavaScript ????????. ? ??? ?? ?? ???? ?? ?? ??(??)? ??? ??? ???? ?? ??? ?? ????. ? ????? jQuery? ???? ? ??? ???? ??? ???? ???? ?? ??? ?????. ?? ???? jQuery ?????? ???? ???. HTML ??? ?? ??? ?????.

jQuery? ???? ??? ?? ??? ???? ??? ?????? ????? ????? ??? ?? ??? ???? ?? ??? ?? ????. ??? ??? ??? ???? ???? ? ?? ?? ??? ?? ??? ???? ?? ??? ????. ? ????? jQuery? ???? ??? ?? ??? ???? ??? ???? ???? ?? ??? ?????. jQuery? ???? ?? ??? ???? ?? ?? CSS? ?? ??? ???? ???. height ??? ??? ??? ???? ? ?????.

jQuery? ? ????? DOM ?? ? ??? ??? ???? ? ?? ???? ?? ?? JavaScript ????????. jQuery?? eq() ???? ??? ??? ???? ??? ???? ? ?????. ???? ?? ? ?? ????? ??? ????. jQuery?? eq() ???? ??? ??? ??? ?? ??? ?????. ??? ??? 0?? ???? ?????. ?, ? ?? ??? ???? 0?? ? ?? ??? ???? 1???. eq() ???? ??? ??? ????: $("s

jQuery? ? ??? ?? ???? ?? ?? JavaScript ????????. ? ?? ?? JavaScript? ?? ???? ? ?? ???? ???? ?? ??? ????. ? ????? jQuery? ???? ???? ? ?? ???? ??? ???? ?? ?? ??? ?????. ?? HTML ???? jQuery ?????? ???? ???. jQuery ?????? ?? ??? ?? ??? ??? ? ????.

jQuery ??? ?? ??? ??? ??? ? ? ???? jQuery? ???? DOM ??? ??? ? ??? ?? ??? ??? ???? ?? ??? ?? ?????. ? ?? jQuery?? ???? ???? ???? ? ??? ?? ??? ? ????. ??? jQuery ??? ?? ??? ??? ???? ?? ????? ???? ? ?? ??? ?? ?? ??? ?? ?????. ?? 1: attr() ???? typeof ???? // ???? ??? ?? ??? ??? ??
