Baidu Front-End Academy Second Phase Task 20
http://ife.baidu.com/2016/tas...
search.addEventListener("click",function(){
let text = searchText.value;//text 用户输入的内容
for(let i= 0;i<data.length;i++){
if(data[i].search(text)===0){
li[i].style.backgroundColor = "yellow";
}
}
});
I saw someone matching the content input by the query user like this. I don’t know what this means. Is it a query for a string that is equal to 0?
Loop through all
data
数组,假如有data
的元素没有找到textif(data[i].search(text)===0)
、就把当前li
Set the background to yellow warning!For details, please click on the js search method: http://www.w3school.com.cn/js...
JS search method: If no matching substring is found, it returns -1; if it is found, it returns the pattern matching template where the first letter of the string to be searched is located ->data[i] in the string Mark index.
So the if() judgment is to hope that text can be matched from the first letter of data[i].
Such as abacab.search(ab), this will return 0 and enter the if block.