需求分析:
  根据关键字搜索网页内容,并且高亮显示内容中的关键字
细节分析:
  1、每次执行搜索操作,需清空上一次结果
  2、需区分html标签和正常文本内容,否则为关键字添加样式以后会出现标签内容被显示的情况
代码思路:
  利用正则表达式匹配关键字
    使用javascript字符串替换的方式,将关键字替换成<span class='red'>关键字</span>
  为了避免出现
    当关键字为 'p' 时候,将标签<p>替换成<<span>p</span>>……等等
  的情况
  所有匹配和替换操作只针对当前DOM元素中文本节点,通过递归函数遍历操作所有节点
前端框架:

angularjs^1.2.9

  1. $scope.myData = '<div>woshihight<h2>womei<b>bbb</b>shihigh<span>haha</span></h2><span>1000pxhight</span><ul><li>1high</li><li>2hight span light hight< p></li></ul><a href="index.html">这个是链接地址hight</a></div>';
  2. $scope.myDataCp = angular.copy($scope.myData);
  3. $scope.key = '';
  4. $scope.searchKey = function() {
  5. if($scope.key != '') {
  6. searchHighLight($scope.key);
  7. }
  8. }
  9.  
  10. function searchHighLight(key) {
  11. var _element = angular.element($scope.myDataCp);
  12. nodeRecursion(_element[0],key);
  13. var _htmlStr = _element[0].innerHTML.toString();
  14. _htmlStr = _htmlStr.replace(/_1000px_/g, '<span class="red">').replace(/_xp0001_/g, '</span>');
  15. $scope.myData = _htmlStr;
  16. }
  17.  
  18. //循环遍历替换所有文本节点内容
  19. function nodeRecursion(e, key) {
  20. var reg = new RegExp(key, 'g');
  21. var _count = e.childNodes.length;
  22. for(var _i=0; _i < _count; _i++) {
  23. if(e.childNodes.item(_i).nodeType == 3) {
  24. var _str = e.childNodes.item(_i).data;
  25.  
  26. if(_str.indexOf(key)!=-1) {
  27. _str = _str.replace(reg,'_1000px_'+key+'_xp0001_');
  28. }
  29. e.childNodes.item(_i).data = _str;
  30. } else {
  31. nodeRecursion(e.childNodes.item(_i), key);
  32. }
  33. }
  34. }

其他说明:

searchKey //点击搜索按钮调用该方法

$scope.myData 中的html字符串必须有一个根节点,比如这里的div

html页面中加载该html字段需要ng-bind-html指令,该指令需要加载ngSanitize模块

Angularjs^1.2.9 搜索关键字高亮显示的更多相关文章

  1. spring data solr 搜索关键字高亮显示

    spring data solr 搜索关键字高亮显示 public Map<String, Object> highSearch(Map searchMap) { Map map = ne ...

  2. 使用spring data solr 实现搜索关键字高亮显示

    后端实现: @Service public class ItemSearchServiceImpl implements ItemSearchService { @Autowired private ...

  3. Input框搜索关键字高亮显示

    ruleTitle(text, val) { if (!val) return text; const result = text.replace( new RegExp(val, "g&q ...

  4. JS将搜索的关键字高亮显示实现代码

    这篇文章介绍了JS将搜索的关键字高亮显示实现代码,有需要的朋友可以参考一下 用JS让文章内容指定的关键字加亮 是这样的.. 现在有这些关键字:美容,生活,购物 当在文章里头出现这些关键字,就把它加亮显 ...

  5. Asp.net 中高亮显示搜索关键字简单方法

    今天用到搜索时的高亮显示,百度了一下,如下面: 1.替换关键字,对字体变色.         public static string ReplaceRed(string strtitle, stri ...

  6. 搜索结果高亮显示(不改变html标签)

      分类: 代码2010-02-28 13:44 1574人阅读 评论(3) 收藏 举报 htmlinputstring 一.问题的产生 搜索结果高亮显示,在新闻标题,来源之类的地方好做,只需要用st ...

  7. 在 Angular 中实现搜索关键字高亮

    在 Angular 中,我们不应该试图直接修改 DOM 的内容,当需要更新 DOM 内容的时候,应该修改的其实是我们的数据模型,也就是 $scope 中的数据,Angular 会帮助我们将修改之后的数 ...

  8. asp实现关键词不区分大小写搜索并高亮显示

    用ASP做搜索很容易,但要实现智能搜索这类就比较累一点,其实任何程序都差不多,主要还是看数据库的处理能力,一般小网站ASP经常跟ACCESS数据库搭配,在这种配置下我们要实现关键词不区分大小写搜索并高 ...

  9. C#分析搜索引擎URL得到搜索关键字,并判断页面停留时间以及来源页面

    前台代码: var start; var end; var state; var lasturl = document.referrer; start = new Date($.ajax({ asyn ...

随机推荐

  1. Codeforces Round #364 (Div. 2)C. They Are Everywhere(尺取法)

    题目链接: C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input ...

  2. Raspberry Pi 上使用WN725N连接WIFI

    系统版本 lee@Lee-RPi ~ $ uname -ar Linux Lee-RPi + # PREEMPT Thu Dec :: GMT armv6l GNU/Linux 这个版本的系统,已经集 ...

  3. PAT (Advanced Level) 1028. List Sorting (25)

    时间卡的比较死,用string会超时. #include<cstdio> #include<cstring> #include<cmath> #include< ...

  4. 让MySQL数据库支持Emoji表情

    问题:Emoji 表情是按照4个字节存储的,所以传统 mysql utf-8编码只能最大存储3字节. 解决:修改MySQL(5.5.3以上版本) 编码为utf8mb4 即可存储Emoji表,同时设置 ...

  5. Lua学习系列(三)

    Ubuntu14.04 上源码编译安装lua5.3 原文:http://blog.csdn.net/abclixu123/article/details/46676991

  6. SecureCRT上传bash: rz: command not found

    SecureCRT上传bash: rz: command not found -bash: rz: command not found rz命令没找到? 执行sz,同样也没找到.     安装lrzs ...

  7. PHP安装插件方式

    PHP安装插件方法主要有两种: 1.先安装相关的库,zlib.curl.xml等,然后在安装 php 时的 ./configure 中设置 --with-xxx(你需要的插件),三部曲安装即可. 2. ...

  8. c++几个新特性

    template 模板 1.出于通用性考虑,程序库中几乎所有东西都被设计为template形式,不支持template几乎不能使用标准程序库. 2.所谓template,是针对"一个或多个尚 ...

  9. 如何在Ubuntu中使用Eclipse + CDT开发C/C++程序

    在Ubuntu中安装Eclipse和CDT步骤如下: 1. 下载资源(都下载到/home/maxw/Download/Eclipse下)    A. 下载JRE(Java Runtime Enviro ...

  10. Vue.js实例

    构造器 每个 Vue.js 应用都是通过构造函数 Vue 创建一个 Vue 的根实例 启动的: var vm = new Vue({ // 选项 })