Files
ClassContent/历届学生/18课程/class18/课程/2020-10-21/搜索建议.html
2024-09-27 02:06:13 +08:00

46 lines
1.2 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" placeholder="请输入关键字">
<ul></ul>
</body>
<script>
var KeyList = ['js学习', '不想学js', 'html和js', 'java面向对象', 'mysql从删库到跑路', '数据查询-mysql'];
var SearchList = [];
var UserInput = document.querySelector("input");
UserInput.onchange = function () {
if (UserInput.value.length != 0) {
SearchList = []
KeyList.forEach((v, i) => {
if (v.indexOf(UserInput.value) > -1) {
var a = v.substr(v.indexOf(UserInput.value), UserInput.value.length)
SearchList.push(v.replace(a, `<span style="color:red;">${a}</span>`))
}
})
RenderLi()
} else {
document.querySelector("ul").innerHTML = "";
}
}
function RenderLi() {
document.querySelector("ul").innerHTML = "";
SearchList.forEach((v, i) => {
document.querySelector("ul").innerHTML += `
<li>${v}</li>
`;
})
}
</script>
</html>