gpt4 book ai didi

javascript - '.contains' 在 Android 手机中不起作用,但在网页中工作正常

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:18:45 25 4
gpt4 key购买 nike

我有一个产品列表,我想通过输入参数搜索产品列表。所以我使用 contains 搜索产品列表中的输入字符串。它在网页上工作正常。但是当我在移动网页中打开同一页面时,它不起作用。并在未定义中给出“包含”的错误。

if(productlist[i].name.toLowerCase().contains(input_val.toLowerCase()))

--my business logic--

之后我尝试使用 indexOf 然后它在这两种情况下都工作正常。

if(productlist[i].name.toLowerCase().indexOf(input_val.toLowerCase()) !== -1)
--my business logic --

.contains 有什么问题??

最佳答案

使用这个 Polyfill,(引用:MDN)

if(!('contains' in String.prototype)) {
String.prototype.contains = function(str, startIndex) {
return -1 !== String.prototype.indexOf.call(this, str, startIndex);
};
}

参见 Compatibility table在我可以使用...

更新:您也可以查看this answer .

关于javascript - '.contains' 在 Android 手机中不起作用,但在网页中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17765310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com