JavaScript 搜索框自动提示
JavaScript 搜索框自动提示
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>filter the value</title>
<style>
#myInput {
background-image: url('/images/mix/searchicon.png');
/* 搜索按钮 */
background-position: 10px 12px;
/* 定位搜索按钮 */
background-repeat: no-repeat;
/* 不重复图片*/
font-size: 16px;
width: 100%;
border: 1px solid #dddddd;
padding: 12px 20px 20px 40px;
margin-bottom: 12px;
}
#myUL {
list-style-type: none;
padding: 0px;
margin: 0px;
}
#myUL li a {
border: 1px solid #ddd;
margin-top: -1px;
background-color: #f6f6f6;
padding: 12px;
text-decoration: none;
font-size: 18px;
color: black;
display: block;
}
#myUL li a.header {
font-weight: bold;
background-color: #e2e2e2;
cursor: default;
}
#myUL li a:hover:not(.header) {
background-color: #eee;
}
</style>
</head>
<body>
<input id="myInput" type="text" placeholder="Search....">
<ul id="myUL">
<li><a href="#" class="header">A</a></li>
<li><a href="#">abc</a></li>
<li><a href="#">abort</a></li>
<li><a href="#">abandon</a></li>
<li><a href="#" class="header">B</a></li>
<li><a href="#">baby</a></li>
<li><a href="#">bird</a></li>
<li><a href="#">bin</a></li>
<li><a href="#" class="header">C</a></li>
<li><a href="#">car</a></li>
<li><a href="#">card</a></li>
<li><a href="#">cavans</a></li>
</ul>
<script>
function myFunction() {
var input, ul, li, a
input = document.getElementById("myInput")
ul = document.getElementById("myUL")
li = document.getElementsByTagName("li")
var inputvalue = input.value.toUpperCase()
for (var i = 0; i < li.length; i++) {
a = li.getElementsByTagName("a")
if (a.innerHTML.toUpperCase().indexOf(inputvalue) > -1) {
li.style.display = ""
}
else {
li.style.display = "None";
}
}
}
</script>
</body>
</html>
如果能把代码的运行效果用视频方式展示下,就更完美了:lol
页:
[1]