如何创建可搜索的按钮列表?

时间:2017-05-15 14:36:24

标签: javascript html css

我有列表格式的按钮。我希望能够使用此搜索脚本或其他搜索脚本搜索按钮。它目前无法使用此代码。

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

    }
  }
}

有没有人有脚本可以让我搜索这些按钮?

1 个答案:

答案 0 :(得分:2)

li项中,没有a个元素。 只需更改此行:

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

为:

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

function myFunction() {
  var input, filter, ul, li, a, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  ul = document.getElementById("myUL");
  li = ul.getElementsByTagName("li");
  for (i = 0; i < li.length; i++) {
    a = li[i].getElementsByTagName("button")[0];
    if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
      li[i].style.display = "";
    } else {
      li[i].style.display = "none";
    }
  }
}
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Please type a keyword." title="FAQ Search">

<ul id="myUL">
  <li><button onclick="document.getElementById('id01').style.display='block'" class="w3-btn w3-white w3-hide-small"><i class="fa fa-info w3-margin-center"></i>How To Use This Site</button>
    <div id="id01" class="w3-modal">
      <div class="w3-modal-content">
        <div class="w3-container">
          <span onclick="document.getElementById('id01').style.display='none'" class="w3-closebtn">&times;</span>
          <p>Hello</p>
        </div>
      </div>
    </div>
  </li>
  <li><button onclick="document.getElementById('id02').style.display='block'" class="w3-btn w3-white w3-hide-small"><i class="fa fa-info w3-margin-center"></i>When to use</button>
    <div id="id02" class="w3-modal">
      <div class="w3-modal-content">
        <div class="w3-container">
          <span onclick="document.getElementById('id02').style.display='none'" class="w3-closebtn">&times;</span>
          <p>Hello</p>
        </div>
      </div>
    </div>
  </li>
</ul>

P.S。当然,考虑到它不是a元素,将变量的名称更改为有意义的东西也会很好......