jq搜索关键字高亮显示

2022-07-28,,,

在我们平时做项目经常会遇到关键字需要高亮显示的情况,算了 不编了,写不下去了,直接看代码吧

 var searchText = keyword;//获取你输入的关键字;
        var regExp = new RegExp(searchText, "g");//创建正则表达式
        $(".searchListBox .prolist-title a").each(function()//遍历文字;
        {
            var html = $(this).html();
            var newHtml = html.replace(regExp, "<span style='color:#dd2421;margin-left:3px;margin-right:3px;' >"+searchText+"</span>");//将找到的关键字替换,加上highlight属性;
    
            $(this).html(newHtml);//更新标题;
    
        });

就是拿到关键词,循环遍历需要替换的地方,然后替换

效果如上

本文地址:https://blog.csdn.net/qq_35168861/article/details/109364345

《jq搜索关键字高亮显示.doc》

下载本文的Word格式文档,以方便收藏与打印。