【问题标题】:how to fix Object doesn't support property or method 'indexOf'?如何修复对象不支持属性或方法'indexOf'?
【发布时间】:2013-10-23 03:07:07
【问题描述】:

我有这种情况:

if (image.indexOf("/bob/") != -1 || image.indexOf("/grabs/") != -1 || image.indexOf("/") == image.lastIndexOf("/")) {
    alert('success');
}

在 IE8 中我得到Object doesn't support property or method 'indexOf'

我可能会使用$.inArray("/bob/", image) 之类的东西,但我不确定lastIndexOf

有什么想法可以解决这个问题吗?

【问题讨论】:

    标签: javascript jquery internet-explorer-8 indexof lastindexof


    【解决方案1】:

    尝试使用正则表达式,例如

    if(/\/(bob|ginger|grabs)\//.test(image) || /^[^\/]*\/$/.test(image)){
    }
    

    【讨论】:

      【解决方案2】:

      如果你想用jQuery解决它,你可以这样做

      $.inArray("/", image, $.inArray("/", image)) === -1
      

      即在第一次出现之后寻找下一次出现的/。这假定/ 始终存在于数组中。如果不是,那么等价的将是

      var index =  $.inArray("/", image);
      if (.... || (index === -1 ||  $.inArray("/", image, index) === -1)) {
         // ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-11
        • 1970-01-01
        • 2011-02-06
        • 2017-09-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多