js报错replace is not a function

出现标题或这样的:Uncaught TypeError: Cannot read properties of undefined (reading ‘replace‘),解决方法有两招:

解决方法1:可先把内容强制转化为字符串,可用value.toString()来强制转换

解决方案2:增加判断,如果是string类型再使用replace函数。

if (typeof(x)==‘string’) {x.replace(…)}

如果x的类型是event,就会报这个错,因为event没有replace方法。