【问题标题】:Cordova javascript error: has no method startsWith AndroidCordova javascript错误:没有方法startsWith Android
【发布时间】:2015-10-13 15:02:42
【问题描述】:

我有两台设备,一台使用 Lollipop,一台使用 Kitekat...使用 Lollipop 的一台没有报告任何错误,但是当我尝试我的应用程序时,我收到此错误:

10-13 16:56:56.126: I/chromium(6322): [INFO:CONSOLE(99)] "Uncaught
TypeError: Object ALIM. IL SOLE DI LOPARDO MARIANGELA has no 
method 'startsWith'", source: file:///android_asset/www/soggetti3.html (99)

这是我的 javascript 的一部分:

 function onDeviceReady() {

        window.rancolor=ranColor();

        var ricerca=localStorage.getItem('testo_ricerca');
        var risultati = JSON.parse(localStorage["risultati"]);

        var ricerca1=ricerca.toString();
        ricerca1=ricerca1.toUpperCase();
        var res_match=[];

        var lll=risultati.length;

        for(var r=0;r<lll;r++){

            var ppp0=risultati[r][1].toString();
            var ppp1=risultati[r][0].toString();
            var ppp2=risultati[r][2].toString();
            var ppp3=risultati[r][3].toString();

            ppp0=ppp0.toUpperCase();
            alert(ppp0);
            alert(ricerca1);
            var check=ppp0.startsWith(ricerca1);

            if(check==true){
                res_match=res_match.concat([[ppp1,ppp0,ppp2,ppp3]]);
            }

        }
        var y=res_match.length;

我应该如何搜索以其他字符串开头的字符串的字符串数组?

【问题讨论】:

  • 请贴一些soggetti3.html的代码
  • 查看编辑,谢谢...

标签: javascript android cordova startswith


【解决方案1】:

实现startWith函数的最简单方法如下所示,然后您可以在代码中使用startWith:

if (typeof String.prototype.startsWith != 'function') {
  // see below for better implementation!
  String.prototype.startsWith = function (str){
    return this.indexOf(str) === 0;
  };
}

【讨论】:

  • 感谢它的工作!但我不明白,对于某些设备,startWith 是定义的,而对于其他设备则不是......代码是一样的!
  • startsWith 是否可用将取决于您的浏览器引擎...看起来 Android 浏览器没有在 JS 中实现这一点,但 Chrome 确实在这里看到了移动标签 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 或者干脆改用str1.indexOf(str2) === 0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-28
  • 2021-10-02
  • 2020-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多