search

Home  >  Q&A  >  body text

javascript - search() method

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?

高洛峰高洛峰2789 days ago1105

reply all(2)I'll reply

  • 迷茫

    迷茫2017-05-18 11:05:10

    Loop through all data数组,假如有data的元素没有找到text if(data[i].search(text)===0)、就把当前liSet the background to yellow warning!

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-18 11:05:10

    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.

    reply
    0
  • Cancelreply