首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

js查找字符串解决方案

2012-05-27 
js查找字符串我想做的共能是:输入正则表达式 网页中如果符合的话就 高亮显示。我先能 根据输入的正则 找到

js查找字符串
我想做的共能是:输入正则表达式 网页中如果符合的话就 高亮显示。 我先能 根据输入的正则 找到相应的字符串。 但是怎么让他高亮呢? 比如:
<html>
<head>
  <title>无标题页</title>
<script>
</head>
<body>
  <form id="form1" runat="server">
  
  <a href="http://paper.wenweipo.com/2012/05/16/CH1205160004.htm" >高亮1</a>
  
  <a href="http://paper.wenweipo.com/2012/05/16/CH12051655555504.htm" >高亮2</a>
  
  <a href="http://paper.wenweipo.com/2012/05/16/CH120516666666604.htm" >高亮3</a>

  </form>
</body>
我用这个正则http://paper.wenweipo.com/2012/05/16/.+.htm是可以找到
  http://paper.wenweipo.com/2012/05/16/CH1205160004.htm
  http://paper.wenweipo.com/2012/05/16/CH12051655555504.htm
  http://paper.wenweipo.com/2012/05/16/CH120516666666604.htm
但是我怎么才能让 高亮1 高亮2 高亮3 这三个字符亮呢?

[解决办法]

HTML code
<html><head>  <title>无标题页</title></head><body>  <form id="form1" runat="server">     <a href="http://paper.wenweipo.com/2012/05/16/CH1205160004.htm" >高亮1</a>     <a href="http://paper.wenweipo.com/2012/05/16/CH12051655555504.htm" >高亮2</a>     <a href="http://paper.wenweipo.com/2012/05/16/CH120516666666604.htm" >高亮3</a>  </form>        <script>            function $(el){                return typeof el == 'string' ? document.getElementById(el) : el;            }            function $t(name, cot){                cot = cot || document;                return cot.getElementsByTagName(name);            }            var s = 'http://paper.wenweipo.com/2012/05/16/.+.htm';            var r = new RegExp(s);            var objs = $t('a', $('form1'));            for( var i = 0, len = objs.length; i < len; i++){                if( r.test(objs[i].href) ){                    objs[i].innerHTML = '<span style="color:red;">'+objs[i].innerHTML+'</span>';                }            }                                            </script>  </body> 

热点排行