实例介绍
【实例简介】
【实例截图】
【核心代码】
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>关键字搜索高亮显示</title>
<style>
.btn
{
background: #f6ad3a;
border: none;
color: #fff;
cursor: pointer;
display: inline-block;
font-family: "Microsoft Yahei";
font-size: 12px;
height: 32px;
line-height: 32px;
margin: 0 1px 0 0;
padding: 0 20px;
}
.keyword
{
display: block;
float: left;
margin: 0;
padding: 0 5px;
width: 110px;
height: 30px;
line-height: 28px;
font-size: 12px;
border: 1px solid #dbdbdb;
color: #444;
}
</style>
</head>
<body>
<div style=" margin-left:40%;">
输入关键字:<input type="text" class="keyword" value="" id="txtKeywords" /><a class="btn"
href="javascript:void(0)" onclick="SearchClick()">搜索</a>
</div>
<div style=" margin-left:40%;">
<input type="checkbox" value="1" name="ra" id="chkUp" />匹配大小写
</div>
<div id="content">
<div style="width: 100%; line-height: 160%; padding: 0 0 0 10%; font-size: 16px;"
id="geci">
<div id="T_0" >
When You Are Gone --avril(艾薇儿)</div>
<div id="T_17">
I always needed time on my own</div>
<div id="T_22">
I never thought
</div>
<div id="T_23">
I'd need you there when I cried</div>
<div id="T_30">
And the days feel like years
</div>
<div id="T_33">
when I'm alone</div>
<div id="T_35">
And the bed where you lie</div>
<div id="T_38">
Is made up on your side</div>
<div id="T_44">
When you walk away</div>
<div id="T_46">
I count the steps that you take</div>
<div id="T_50">
Do you see how much I need you right now?</div>
<div id="T_56">
When you're gone</div>
<div id="T_59">
The pieces of my heart
</div>
<div id="T_61">
are missing you</div>
<div id="T_63">
When you're gone</div>
<div id="T_65">
The face I came to know
</div>
<div id="T_68">
is missing too</div>
<div id="T_70">
When you're gone</div>
<div id="T_72" >
The words I need to hear
</div>
<div id="T_74" >
to always get me through the day</div>
<div id="T_79" >
And make it ok</div>
<div id="T_83" >
I miss you</div>
<div id="T_98" >
I never felt this way before</div>
<div id="T_103" >
Everything that I do</div>
<div id="T_106" >
reminds me of you</div>
<div id="T_111" >
And the clothes you left</div>
<div id="T_113" >
are lyin' on the floor</div>
<div id="T_116" >
And they smell just like you</div>
<div id="T_119" >
I love the things that you do</div>
<div id="T_125" >
When you walk away</div>
<div id="T_128" >
I count the steps that you take</div>
<div id="T_131" >
Do you see how much
</div>
<div id="T_133" >
I need you right now?</div>
<div id="T_138" >
When you're gone</div>
<div id="T_140" >
The pieces of my heart
</div>
<div id="T_143" >
are missing you</div>
<div id="T_144" >
When you're gone</div>
<div id="T_147" >
The face I came to know
</div>
<div id="T_149" >
is missing too</div>
<div id="T_151" >
When you're gone</div>
<div id="T_153" >
The words I need to hear
</div>
<div id="T_155" >
to always get me through the day</div>
<div id="T_160" >
And make it OK</div>
<div id="T_164" >
I miss you</div>
<div id="T_167" >
We were meant for each other</div>
<div id="T_170" >
I keep forever</div>
<div id="T_174" >
I know we were</div>
<div id="T_179" >
All I ever wanted was for you to know</div>
<div id="T_182" >
Everything I do I give my heart and soul</div>
<div id="T_186" >
I can hardly breathe
</div>
<div id="T_187" >
I need to feel you here with me</div>
<div id="T_191" >
Yeah</div>
<div id="T_192" >
When you're gone</div>
<div id="T_194" >
The pieces of my heart are missing you</div>
<div id="T_198" >
When you're gone</div>
<div id="T_201" >
The face I came to know
</div>
<div id="T_203" >
is missing too</div>
<div id="T_205" >
When you're gone</div>
<div id="T_207">
The words I need to hear
</div>
<div id="T_209" >
to always get me through the day</div>
<div >
And make it OK</div>
<div id="T_218">
I miss you
</div>
</div>
</div>
</body>
</html>
<script>
function InitialKeywordsShow(_keywords, obj, caseTrue) {
//var reg = new RegExp(_keywords, "g");//区分大小写
var reg = new RegExp(_keywords, "gi"); //不区分大小写
if (caseTrue) {
var reg = new RegExp(_keywords, "g");
}
if (obj && obj.childNodes && obj.childNodes.length > 0) {
for (var i = 0; i < obj.childNodes.length; i ) {
if (obj.childNodes[i].nodeType == 3 && obj.childNodes[i].nodeValue && obj.childNodes[i].nodeValue.length > 0) {
var txtnew = obj.childNodes[i].nodeValue.replace(reg, "<font color='red' style=' background:#e48b00;color:#fff;'>" _keywords "</font>");
var _span = document.createElement("span");
_span.innerHTML = txtnew;
obj.replaceChild(_span, obj.childNodes[i]);
}
else if (obj.childNodes[i].nodeType == 1) {
InitialKeywordsShow(_keywords, obj.childNodes[i], caseTrue);
}
}
}
}
function SearchClick() {
var fonts = document.getElementsByTagName("font");
if (fonts && fonts.length > 0) {
for (var i = 0; i < fonts.length; i ) {
fonts[i].style.backgroundColor = "#fff";
fonts[i].style.color = "black"
}
}
var keyWords = document.getElementById("txtKeywords").value;
var obj_content = document.getElementById("content");
if (!keyWords) {
alert("请输入关键字");
return false;
}
var upTrue = document.getElementById("chkUp").checked;
InitialKeywordsShow(keyWords, obj_content, upTrue);
}
</script>
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论