在Chrome中使用getElementsByClassName的JavaScript过滤器

时间:2017-05-06 21:04:36

标签: javascript flask jinja2

根据W3Schools的此页面,我将li = ul.getElementsByTagName('li');更改为li = ul.getElementsByClassName('iteminlist');。这在Safari和Firefox中适用于我。但是,JavaScript过滤器无法在Chrome中运行。任何想法为什么会这样,我如何改变它以使其兼容Chrome?

function filter() {

    var input, filter, ul, li, a, i;
    input = document.getElementById('myInput');
    filter = input.value.toUpperCase();
    ul = document.getElementById("itemlist");
    li = ul.getElementsByClassName('iteminlist');

    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";
        }
    }
}

HTML Jinja2模板如下:

 <div id="itemlist">
    {% for item in menu %}
      <div class='iteminlist'><a href="#">{{ item.name }}</a> has title {{ item.title }} and type {{ item.type }}</div>
    {% endfor %}

HTML代码输出:

    <div id="itemlist">

          <div class="iteminlist"><a href="#">one</a> has title 1 and type A</div>

          <div class="iteminlist"><a href="#">two</a> has title 2 and type B</div>

          <div class="iteminlist"><a href="#">three</a> has title 3 and type C</div>

          <div class="iteminlist"><a href="#">four</a> has title 4 and type D</div>

          <div class="iteminlist"><a href="#">five</a> has title 5 and type E</div>

          <div class="iteminlist"><a href="#">six</a> has title 6 and type F</div>

          <div class="iteminlist"><a href="#">seven</a> has title 7 and type G</div>

          <div class="iteminlist"><a href="#">eight</a> has title 8 and type H</div>

          <div class="iteminlist"><a href="#">nine</a> has title 9 and type I</div>

          <div class="iteminlist"><a href="#">ten</a> has title 10 and type J</div>

   </div>

2 个答案:

答案 0 :(得分:0)

只是一个基本的想法:你的HTML是怎样的? 列表项是否具有类iteminlist,例如:

<li class="iteminlist">

答案 1 :(得分:0)

似乎为我工作: 每个li都有li = ul.getElementsByClassName("iteminlist");<li class="iteminlist">

&#13;
&#13;
function myFunction() {
  var input, filter, ul, li, a, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  ul = document.getElementById("myUL");
  li = ul.getElementsByClassName("iteminlist");
  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";

    }
  }
}
&#13;
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 12px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

#myUL li a {
  border: 1px solid #ddd;
  margin-top: -1px;
  /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block
}

#myUL li a.header {
  background-color: #e2e2e2;
  cursor: default;
}

#myUL li a:hover:not(.header) {
  background-color: #eee;
}
&#13;
<h2>My Phonebook</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">

<ul id="myUL">
  <li class="iteminlist"><a href="#" class="header">A</a></li>
  <li class="iteminlist"><a href="#">Adele</a></li>
  <li class="iteminlist"><a href="#">Agnes</a></li>

  <li class="iteminlist"><a href="#" class="header">B</a></li>
  <li class="iteminlist"><a href="#">Billy</a></li>
  <li class="iteminlist"><a href="#">Bob</a></li>

  <li class="iteminlist"><a href="#" class="header">C</a></li>
  <li class="iteminlist"><a href="#">Calvin</a></li>
  <li class="iteminlist"><a href="#">Christina</a></li>
  <li class="iteminlist"><a href="#">Cindy</a></li>
</ul>
&#13;
&#13;
&#13;