本章节我们将学习如何使用 JS/CSS 实现搜索框联想功能。
先看下效果如下:
查看在线实例:https://c.runoob.com/codedemo/6190
基础 HTML 代码
实例
form autocomplete=“off“ action=“/index.php“>
div class=“autocomplete“ style=“width:300px;“>
input id=“myInput“ type=“text“ name=“s“ placeholder=“请输入搜索内容“>
div>
input type=“submit“>
form>
再准备基础的搜索测试数据,是一个 JavaScript 数组:
var sites = ["Google","Taobao","Runoob","Wiki","Zhihu","Baidu","Sina","Tmall","JD","Alibaba","QQ","Weixin"];
以下搜索搜索框和联想菜单的样式:
实例
* { box-sizing: border-box; }
body {
font: 16px Arial;
}
.autocomplete {
/*容器定位设置为 relative:*/
position: relative;
display: inline-block;
}
input {
border: 1px solid transparent;
background-color: #f1f1f1;
padding: 10px;
font-size: 16px;
}
input[type=text] {
background-color: #f1f1f1;
width: 100%;
}
input[type=submit] {
background-color: DodgerBlue;
color: #fff;
}
.autocomplete-items {
position: absolute;
border: 1px solid #d4d4d4;
border-bottom: none;
border-top: none;
z-index: 99;
/*设置自动填充项宽度与容器相同*/
top: 100%;
left: 0;
right: 0;
}
.autocomplete-items div {
padding: 10px;
cursor: pointer;
background-color: #fff;
border-bottom: 1px solid #d4d4d4;
}
.autocomplete-items div:hover {
/*鼠标移动到填充项设置的背景颜色*/
background-color: #e9e9e9;
}
.autocomplete-active {
/*使用箭头键浏览填充项时的背景颜色*/
background-color: DodgerBlue !important;
color: #ffffff;
}
body {
font: 16px Arial;
}
.autocomplete {
/*容器定位设置为 relative:*/
position: relative;
display: inline-block;
}
input {
border: 1px solid transparent;
background-color: #f1f1f1;
padding: 10px;
font-size: 16px;
}
input[type=text] {
background-color: #f1f1f1;
width: 100%;
}
input[type=submit] {
background-color: DodgerBlue;
color: #fff;
}
.autocomplete-items {
position: absolute;
border: 1px solid #d4d4d4;
border-bottom: none;
border-top: none;
z-index: 99;
/*设置自动填充项宽度与容器相同*/
top: 100%;
left: 0;
right: 0;
}
.autocomplete-items div {
padding: 10px;
cursor: pointer;
background-color: #fff;
border-bottom: 1px solid #d4d4d4;
}
.autocomplete-items div:hover {
/*鼠标移动到填充项设置的背景颜色*/
background-color: #e9e9e9;
}
.autocomplete-active {
/*使用箭头键浏览填充项时的背景颜色*/
background-color: DodgerBlue !important;
color: #ffffff;
}
以下是搜索搜索框和联想菜单的 JavaScript 代码:
实例
function autocomplete(inp, arr) {
/*自动填充函数有两个参数,input 输入框元素和自动填充的数组*/
var currentFocus;
/* 监听 input 输入框,当在 input 输入框元素中时执行以下函数*/
inp.addEventListener(“input“, function(e) {
var a, b, i, val = this.value;
/*关闭已打开的自动填充列表*/
closeAllLists();
if (!val) { return false;}
currentFocus = –1;
/*创建 DIV 元素用于放置自动填充列表的值*/
a = document.createElement(“DIV“);
a.setAttribute(“id“, this.id + “autocomplete-list“);
a.setAttribute(“class“, “autocomplete-items“);
/*DIV 作为自动填充容器的子元素*/
this.parentNode.appendChild(a);
/*循环数组*/
for (i = 0; i arr.length; i++) {
/*检查填充项是否有与文本字段值相同的内容,不区分大小写*/
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
/*为每个匹配元素创建一个 DIV 元素 */
b = document.createElement(“DIV“);
/*匹配项加粗*/
b.innerHTML = ““ + arr[i].substr(0, val.length) + ““;
b.innerHTML += arr[i].substr(val.length);
/*选中的填充项插入到隐藏 input 输入字段,用于保存当前选中值*/
b.innerHTML += “
/*自动填充函数有两个参数,input 输入框元素和自动填充的数组*/
var currentFocus;
/* 监听 input 输入框,当在 input 输入框元素中时执行以下函数*/
inp.addEventListener(“input“, function(e) {
var a, b, i, val = this.value;
/*关闭已打开的自动填充列表*/
closeAllLists();
if (!val) { return false;}
currentFocus = –1;
/*创建 DIV 元素用于放置自动填充列表的值*/
a = document.createElement(“DIV“);
a.setAttribute(“id“, this.id + “autocomplete-list“);
a.setAttribute(“class“, “autocomplete-items“);
/*DIV 作为自动填充容器的子元素*/
this.parentNode.appendChild(a);
/*循环数组*/
for (i = 0; i arr.length; i++) {
/*检查填充项是否有与文本字段值相同的内容,不区分大小写*/
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
/*为每个匹配元素创建一个 DIV 元素 */
b = document.createElement(“DIV“);
/*匹配项加粗*/
b.innerHTML = ““ + arr[i].substr(0, val.length) + ““;
b.innerHTML += arr[i].substr(val.length);
/*选中的填充项插入到隐藏 input 输入字段,用于保存当前选中值*/
b.innerHTML += “