关键字搜索(练习)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input type="text" id="txt">
<button id="btn">搜索</button>
<span id="show"></span>

<script>
function $(id){
return document.getElementById(id);
}
var txt=$("txt");
var btn=$("btn");
var show=$("show");

var ci=["i love you","i like you","i need you","i want you","i miss you"];
btn.onclick=function(){

if(txt.value==""){
show.innerHTML="<br />请输入搜索内容!";
}
else if(txt.value!="i"){
show.innerHTML="<br />此问题无解!";
}
else{
show.innerHTML="";
for(var j=0;j<ci.length;j++){
if(ci[j].indexOf("i")!=-1){
var bq=document.getElementsByTagName("span");
show.innerHTML+=("<br />"+ci[j].replace(/i/g,"<span style='color:red;'>" +"i"+"</span>"));

}
}

}


}
</script>
</body>
</html>

posted @ 2017-04-19 23:29  非凡。  阅读(219)  评论(0编辑  收藏  举报