數字每隔四位用空格分隔代碼實例:
分享一段代碼實例,它實現了數字每隔四位就用空格分隔。
這樣的效果在填寫銀行卡的時候十分常見,這也是非常人性化的舉措。
代碼實例如下:
<!doctype html><html><head><meta charset="utf-8"><title>VEVB武林網</title><script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script><script>$(document).ready(function () { $('#ant').on('keyup mouseout input', function () { var $this = $(this); var v = $this.val(); //S{5}/.test(v) && $this.val(v.replace(//s/g, '').replace(/(.{4})/g, "$1 ")); });})</script></head><body><input type="text" id="ant" /></body></html>
上面的代碼實現了我們的要求,更多內容可以參閱相關閱讀。
相關閱讀:
(1).on()可以參閱jquery on()綁定事件處理函數詳解一章節。
(2).keyup事件可以參閱jQuery keyup事件一章節。
(3).val()方法可以參閱jQuery val()方法一章節。
(4).test()方法可以參閱正則表達式test()函數一章節。
(5).replace()可以參閱正則表達式replace()函數一章節。
(6).$1可以參閱正則表達式replace()函數一章節。
(7).子表達式可以參閱正則表達式分組一章節。
新聞熱點
疑難解答