纯前端如何实现搜索关键字功能,且为搜索关键字添加颜色

《纯前端如何实现搜索关键字功能,且为搜索关键字添加颜色》

  <div class="select-content">
        <div class="nameShow" title="{
  {item.name}}" 
               [innerHTML]="item.nameHtml">
        </div>
         <div class="deptName" title="{
  {item.deptName}}">
            {
  {item.deptName}}
         </div>
   </div>
     .select-content {
                  display: flex;
                  flex-direction: column;
                  width: 70%;
                  margin-left: 10%;
                  text-align: left;
                  overflow: hidden;
                  text-overflow: ellipsis;
                  white-space: nowrap;


                  .nameShow {
                    width: 100%;
                    text-align: left;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                    margin-top: 2px;
                  }

                  .deptName {
                    font-size: 14px;
                    text-align: left;
                    line-height: 16px;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                    color: #ccc;
                    margin-top: 2px;
                  }
                }
  if(th.personnelSearchKeyword){
        th.rosterList =data.data  
         th.rosterList.forEach((item)=>{
          if (item.hasOwnProperty("name") && item.name) {
             var reg = new RegExp(th.personnelSearchKeyword.replace(/[-\[\]\/\{\}\ 
                       (\)\*\+\?\.\\\^\$\|]/g, "\\$&"), "igm");
             let html = item.name.replace(reg, (txt) => {
                  return '<span style="color:#18aeff;">' + txt + '</span>';
                 })
              item.nameHtml = this.sanitizer.bypassSecurityTrustHtml(html);
            } else {
              item.nameHtml = "";
            }
        })
     }

    原文作者:呆萌小菜鸟
    原文地址: https://blog.csdn.net/m0_59893114/article/details/122670289
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞