在点击javascript之前,不要显示搜索栏

时间:2018-05-27 04:20:39

标签: javascript html css

我正在尝试创建搜索列表的搜索栏。现在,每当我尝试搜索时,列表都会显示。我希望在按下搜索栏时显示列表。我在网页加载后尝试隐藏列表,然后在搜索栏上设置onclick功能以显示列表。这就是我到目前为止所拥有的。



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

        }
    }
}
var too = document.getElementById("myUL").style.display = "none";
function textFunc() {
    too.style.display = "inline";
}
&#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:hover:not(.header) {
  background-color: #eee;
}
&#13;
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<h2>My Phonebook</h2>

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

<ul id="myUL">
  <li><a href="#">Adele</a></li>
  <li><a href="#">Agnes</a></li>

  <li><a href="#">Billy</a></li>
  <li><a href="#">Bob</a></li>

  <li><a href="#">Calvin</a></li>
  <li><a href="#">Christina</a></li>
  <li><a href="#">Cindy</a></li>
</ul>
</body>
</html>
&#13;
&#13;
&#13;

我给你的代码来自W3学校。

1 个答案:

答案 0 :(得分:0)

更改

List< Student > stuList= new ArrayList< Student >( );
StringBuilder sb = new StringBuilder( );
sb.append( "select s.studentId, s.name, s.lastName, b.name " );
sb.append( "from student s " );
sb.append( "inner join borrowedbook c on 
            s.studentId = c.studentId " );
sb.append( "inner join books b on c.bookId = b.bookId " );

Query query = getSession( ).createSQLQuery( sb.toString( ) );
query.setProperties( Student.class );

List< Object[] > searchResults = query.list( );

var too = document.getElementById("myUL").style.display = "none";
function textFunc() {
  too.style.display = "inline";
}

工作代码示例:

&#13;
&#13;
document.getElementById("myUL").style.display = "none";
function textFunc() {
  var too = document.getElementById("myUL");
  too.style.display = "inline";
}
&#13;
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";

        }
    }
}
document.getElementById("myUL").style.display = "none";
function textFunc() {
  var too = document.getElementById("myUL");
  too.style.display = "inline";
}
&#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:hover:not(.header) {
  background-color: #eee;
}
&#13;
&#13;
&#13;