我试图仅隐藏正在显示的文档中的名称。比如说我在html.erb文件中:

<span class="demo-text">My name is Mike and there are seven more people with name Mike in this building </span>


现在,我只希望整个文本中的名称“ Mike”不显示。以下是我未完成的尝试:

.css文件

 .demo-text {

}

 .index-pg {
   color: black;
   background: black;
   height: 20px;
 }


jQuery文件:

$(".demo-text").addClass('index-pg') ;


我实际上是在使用一个按钮来添加或删除“ index-pg”类,以便可以控制何时必须将该名称清空。添加和删​​除类部分工作正常。我在使用jquery进行编码时遇到问题,它会遍历各个单词并查找所有名称“ Mike”,并且仅将类应用于该关键字。

最佳答案

干得好

$(".demo-text").html($(".demo-text").html().replace(/Mike/g, "<span class='index-pg'>Mike</span>"));

关于jquery - 仅使用jquery将css样式应用于关键字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55294291/

10-10 22:04