Files
ClassContent/历届学生/front-end-team-10/项目练习/学生项目/冯凯/练习/js练习/搜索.html
2024-09-27 02:06:13 +08:00

94 lines
2.7 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>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<input type="text" placeholder="请输入关键字">
<button>搜索</button>
<ul>
</ul>
</body>
<script>
var keyWordList = [
'今天出太阳了', '太阳很好', '不喜欢js学习',
'js编程很简单', '喜欢很简单的html', '今天不想学习html,只想出去玩',
'今天不想学习css', '今晚出去飙车', '明天出去修车'
];
var newKeyList = []
$("input").oninput = () => {
newKeyList = []
// 这个 newKeyList = [] 写在这里目的实现清空
if ($("input").value.length > 0) {
// 把keyWordList里的值循环
// 里面的if进行判断符合条件的数据 incledes push放进新的数组当中
//v就是数组加下标
keyWordList.forEach(v => {
if (v.includes($("input").value)) {
var text = v.replace($("input").value, `<span style="color:red;">${$("input").value}</span>`)
newKeyList.push(text)
}
})
console.log("符合条件的数据:", newKeyList);
renderUl()
} else {
$("ul").innerHTML = ""
}
}
//渲染页面 第四步把符合条件的 展示 在 ul li 里面给用户看
function renderUl() {
//把新的li插入到ul里
//innerHTML 父元素内部的html
$("ul").innerHTML = ""//这一步用来清空ul里的值,防止数组累计
//将新的获取的newKeyList进行循环
newKeyList.forEach(v => {
$("ul").innerHTML += `<li>${v}</li>`
})
bindLiClick()
}
//第五步
function bindLiClick() {
//这边_ 得到的一个数组
//三次循环给三次li绑定事件
_("ul li").forEach(v => {
v.onclick = () => {
console.dir(v);
$("input").value = v.innerText
SearchBaidu(v.innerText)
}
})
}
//给button绑定点击事件
$("button").onclick = () => SearchBaidu($("input").value)
function SearchBaidu(text) {
open(`https://www.baidu.com/s?wd=${text}`)
}
function $(className) {
return document.querySelector(className)
}
function _(className) {
return document.querySelectorAll(className)
}
</script>
</html>