简体   繁体   中英

Learning how to create a search menu to filter links

I'm using w3schools example and in the code below i am trying to understand what this line on code means

a = li[i].getElementsByTagName("a")[0];

and also

li[i].style.display = "";

Here is the full code:

function myFunction() {
  // Declare variables
  var input, filter, ul, li, a, i;
  input = document.getElementById("mySearch");
  filter = input.value.toUpperCase();
  ul = document.getElementById("myMenu");
  li = ul.getElementsByTagName("li");

  // Loop through all list items, and hide those who don't match the search query
  for (i = 0; i < li.length; i++) {
    a = li[i].getElementsByTagName("a")[0];
    if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
      li[i].style.display = "";
    } else {
      li[i].style.display = "none";
    }
  }
}
a = li[i].getElementsByTagName("a")[0];

Gets the first <a> tag of the <li> element in the current iteration.

li[i].style.display = "";

Clears the display style attribute of the <li> element in the current iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM