我想手动创建自动建议元素.我正在使用canjs这个程序.
以下代码我到目前为止尝试过:
list.filter( function( item, index, list ) {
if(item.includes(searchText) && searchText != ''){
//css hide and show classes for match
}
else{
// css show for unmatched results
}
})
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我面临两个问题:
包含不适用于所有浏览器.为此,我尝试了匹配,
包含和子字符串,但他们无法帮助我.
包括在chrome中工作,但当我输入其子字符串未包含在list的最后一个元素的字符串时,它将无法
工作,因为filter将继续搜索所有元素.
我有什么错误吗?
我希望它能在所有浏览器中运行.
谢谢.
小智 1
String.prototype.includes()使用一个参数相当于!!~将运算符应用于String.prototype.indexOf(),并且后一种方法适用于所有浏览器。所以你的测试线可以是这样的:
if(!!~item.indexOf(searchText) && searchText !== '\xe2\x80\x99){
| 归档时间: |
|
| 查看次数: |
135 次 |
| 最近记录: |