javascript改变特殊指定字符的背景颜色

来源:互联网 发布:数据线路的通讯方式 编辑:程序博客网 时间:2024/06/14 03:43

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
        <script language="javascript" type="text/javascript">
    function CheckFrm()
    {
          var html_=document.getElementById("Htmlcode").value;  //存放html code
           //document.getElementById("div1").innerHTML = "<b>aaaa</b>";
          var array_=document.getElementById("str_array").value;   //存放关键变量数组
           var strArray= new Array();
           strArray=array_.split(",");
            //var  html_1=html_;    
          var html_1=htmlEncode(html_);
             var aa;
             var re;
           for(i=0;i<strArray.length;i++)
           {
              //if(html_.indexOf(strArray[i])>-1)
             // {
               //document.getElementById("div1").innerHTML = "<b>aaaa</b>";
               // document.write(strArray[0]+"<br>");
                aa=strArray[i].trim();
                re=new RegExp(aa,"g");
                html_1=html_1.replace(re,"<span style='color:#F90'>$&</span>");
               //alert(strArray[i]);
             // }
           }
            document.write(html_1);

    }
    String.prototype.trim = function()
    {
      var reExtraSpace = /^/s*(.*?)/s+$/;
      return this.replace(reExtraSpace,"$1");
    }

    var htmlEncode=function(s){
    var t=document.createElement("div");
    t.appendChild(document.createTextNode(s))
    return t.innerHTML;
    };
    </script>
   
</head>
<body>
    <form id="form1" runat="server">
    <div id="div1">
        <input id="Htmlcode" runat="server" type="hidden"  value="<html><title>##AA##</title><header></header><body>##BB##</body></html>##CC##"/>
        <input id="str_array" runat="server" type="hidden"  value="##AA##,##BB##,##CC##"/>
        <script type="text/javascript">CheckFrm();</script>
    </div>
    </form>
</body>
</html>


------------------------------------------------------------------

<html>
<head> 
<script language="javascript" type="text/javascript">
var htmlEncode=function(s){
    var t=document.createElement("div");
    t.appendChild(document.createTextNode(s))
    return t.innerHTML;
};
function CheckFrm()
    {
var hidden1="<html><title>##AA##</title><header></header><body>##AA##</body></html>##AA##";
hidden1=htmlEncode(hidden1);
var aa="##AA##";
var re=new RegExp(aa,"g");  
hidden1=hidden1.replace(re,"<span style='color:#F90'>$&</span>");
document.write(hidden1);
}
</script>

</head>
<body>
 <form id="form1">
 <script type="text/javascript">CheckFrm();</script>
   </form>
</body>
</html>

 

 

原创粉丝点击