基本信息
源码名称:js实现关键字高亮
源码大小:8.03KB
文件格式:.html
开发语言:C#
更新时间:2015-11-16
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

<!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>