在开发移动端项目中,使用fastclick的时候,给input设置type="number",控制台会抛错,但不影响使用,强迫症患者要去掉这个报错,解决方法如下:

fastclick.js?bf9a:331 Uncaught DOMException: Failed to execute 'setSelectionRange' on 'HTMLInputElement': The input element's type ('number') does not support selection

解决方法:

找到node_module中的文件fastclick.js, line: 327 将

if (deviceIsIOS && targetElement.setSelectionRange && targetElement.type.indexOf('date') !== 0 && targetElement.type !== 'time' && targetElement.type !== 'month') {
            length = targetElement.value.length;
            targetElement.setSelectionRange(length, length);
        } else {
            targetElement.focus();
    }

替换为

var useSelectionRange = deviceIsIOS;
    if(useSelectionRange){
        try{
            length = targetElement.value.length;
            targetElement.setSelectionRange(length, length);
        }catch(error){
            useSelectionRange = false;
        }
    }
    if (!useSelectionRange) {
        targetElement.focus();
    }

Mark
52 声望4 粉丝

用心分享,做有梦想的攻城狮;