解決這個報錯:Failed to execute 'setSelectionRange' on 'HTMLInputElement': The input element's type ('number') does not support selection.


因為在input標簽用了type="number", 導致報錯;

說明:從chrome 33版本開始, chrome瀏覽器只支持獲取type為text, search, URL, tel and password的input元素的selectionStart, selectionEnd 和 setSelectionRange 屬性, 在其余類型中嘗試獲取這些屬性chrome會提示錯誤。

解決方法:

  在node_modules中找到fastclick.js文件

  

將大約在329行的內容:

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();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM