首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用AngularJS在用html标签分割时突出显示搜索到的文本?

如何使用AngularJS在用html标签分割时突出显示搜索到的文本?
EN

Stack Overflow用户
提问于 2018-05-31 00:40:26
回答 1查看 0关注 0票数 0

我正试图在网页上突出显示搜索到的文字。我以json响应的形式从服务器获取内容到网页。在JSON响应中。我得到两个HTML响应与标签和非HTML响应。在我的HTML响应。有很多span标签是用空span标签拆分单个词的。例如:“搜索文本”的格式为“ <span class="c1">Sea</span>rch<span class="c2">text</span>”。分词在接收到的词语响应中的各个位置发生。

我正在使用角度高光。“ angular-highlight ”突出显示未被html标签分割的单词。如果单词被分割,它不会突出显示。但是,它仍然不起作用。

<span>Sample</span> - 将突​​出显示

<span>Sa<span>mp</span>le</span> - 不会突出显示

<span>Sa<span></span>mple<span></span></span> - 不会突出显示

我也使用了trustAsHtml方法。但是,它不工作。

EN

回答 1

Stack Overflow用户

发布于 2018-05-31 09:51:08

代码:

代码语言:javascript
复制
angular.module('angular-highlight', []).directive('highlight', function() {
var matchCount = 0;
var newCheckKeyword = '';
var component = function(scope, element, attrs) {

    if (!attrs.highlightClass) {
                    attrs.highlightClass = 'angular-highlight';
    }
    var count = 0;
    var replacer = function(match, item) {
                    count++;
                    var temp = matchCount+count;
                    return '<span class="'+attrs.highlightClass+'" id="highlightId'+temp+'">'+match+'</span>';
    }


    var tokenize = function(keywords) {
                    keywords = keywords.replace(new RegExp('^ | $','g'), '');
                    return keywords;
    }

    scope.$watch('keywords', function(newKeyword, oldKeyword) {
                    if(newCheckKeyword == '' || newCheckKeyword != newKeyword){
                                    newCheckKeyword = newKeyword;
                                    matchCount = 0;
                                    count = 0;
                                    scope.sendCount({count: matchCount});
                    }
                    //console.log("scope.keywords",scope.keywords);
                    if (!scope.keywords || scope.keywords == '') {
                                    element.html(scope.highlight);
                                    return false;
                    }

                    var tokenized    = tokenize(scope.keywords);
                    // Modified to skip HTML tags
              var regex = new RegExp(tokenized+'(?!([^<]+)?>)', 'gmi');

                    // Find the words
                    var html = scope.highlight.replace(regex, replacer);

                    //var htmlCount = scope.highlight.match(regex, replacer);

                    element.html(html);
                    matchCount = matchCount+count;
                    scope.sendCount({count: matchCount});
                    count = 0;
    });

}
return {
    link: component,
    replace: false,
    scope:{
          highlight:'=',
          keywords:'=',
          sendCount:'&'                                                
    }
};
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100004629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档