This question already has answers here:
Replace method doesn't work
                                
                                    (4个答案)
                                
                        
                                2年前关闭。
            
                    
为什么String.replace()无法与事件一起使用?

var el = document.querySelector('input')
var pat = /(\d{3})(\d{3})(\d{4})/;

el.oninput = function(){
  this.value.replace(pat, '$1-$2-$3')
}

最佳答案

好的,您不需要分配它或返回它,因为您需要设置新值:

var el = document.querySelector('input')
var pat = /(\d{3})(\d{3})(\d{4})/;

el.oninput = function(){
  this.value = this.value.replace(pat, '$1-$2-$3')
}

关于javascript - 为什么String.replace()对事件不起作用? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47416599/

10-11 09:20
查看更多