Files
ClassContent/历届学生/吴欣阳/项目开发/上课项目/js案例/搜索建议.html
2024-09-27 02:06:13 +08:00

63 lines
1.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>搜素建议</title>
<style>
body {
margin: 0;
padding: 0;
}
ul li span {
color: red;
}
</style>
</head>
<body>
<input type="text" placeholder="请输入关键词">
<ul>
</ul>
</body>
<script>
var keyword = document.querySelector("input");
var list = ['html学习', '不想学习', '想吃饭', '我没吃饱', '不写代码', '我的学习代码没写完,懒得学习'];
var ul = document.querySelector("ul");
var NewsList = []
// 1: 实现搜索列表的展示
// 获取用户在输入框中输入的数值
// 拿到数值和数组中每条数据进行判断,符合条件单独保存起来
// 2:给关键字加红色
// 想
keyword.oninput = function () {
NewsList = [];
if (keyword.value.length != 0) {
list.forEach(function (v) {
if (v.includes(keyword.value)) {
v = v.replaceAll(keyword.value, `<span>${keyword.value}</span>`)
NewsList.push(v)
}
});
ul.innerHTML = ""
NewsList.forEach(function(v) {
ul.innerHTML += `<li>${v}</li>`
})
console.log(NewsList);
} else {
ul.innerHTML = ""
}
}
</script>
</html>