Files
ClassContent/历届学生/苏琪/项目练习/weather/search.html
2024-09-27 02:06:13 +08:00

53 lines
1.1 KiB
HTML

<!DOCTYPE html>
<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>搜索建议</title>
<style>
span {
color: red;
}
</style>
</head>
<body>
<input type="text" placeholder="输入关键词" oninput="getInputValue()">
<ul>
</ul>
<script>
var keyWord = [
'今天不好','每天开心','每日一课','心情不好'
];
var list = [];
function getInputValue() {
list = []
var key = document.querySelector("input").value;
if (key.length != 0) {
keyWord.forEach(val => {
if(val.indexOf(key) > -1) {
var res = val.replace(new RegExp(key,'g'),`<span>${key}</span>`);
list.push(res)
}
});
document.querySelector("ul").innerHTML = ''
list.forEach(val => {
document.querySelector("ul").innerHTML += `<li>${val}</li>`
})
} else {
document.querySelector("ul").innerHTML = ''
}
}
</script>
</body>
</html>