Files
ClassContent/历届学生/fontendseven/项目练习/上课项目/搜索建议/index.html
2024-09-27 02:06:13 +08:00

54 lines
1.3 KiB
HTML

<!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>Document</title>
</head>
<body>
<input type="text" placeholder="输入关键字">
<ul>
</ul>
</body>
<script>
var keyword = ['今天天气不好', '今天很开心', '大事不好','我不开心', '天气很一般'];
var newWord = [];
$("input").oninput = () => {
if ($("input").value.length != 0) {
newWord = []
keyword.forEach(v => {
if (v.includes($("input").value)) {
var news = v.replaceAll(
$("input").value,
`<span style="color:red;">${$("input").value}</span>`
)
newWord.push(news)
}
});
renderLi()
}
}
function renderLi() {
$("ul").innerHTML = ""
newWord.forEach(v => {
$("ul").innerHTML += `<li>${v}</li>`
})
}
function $(className) {
return document.querySelector(className)
}
// 箭头函数和传统的函数除了写法的不一样,还有其他吗?
// 箭头函数解绑this指向
</script>
</html>