为什么String.replace()不能处理事件?

时间:2017-11-21 15:22:25

标签: javascript

为什么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')
}

1 个答案:

答案 0 :(得分:1)

好的,你没有指定或返回,因为你需要设置新值

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')
}