首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >突出显示包含以下内容的字符串

问突出显示包含以下内容的字符串
EN

Stack Overflow用户
提问于 2018-03-12 20:23:36
回答 4查看 62关注 0票数 0

我想加粗所有包含运行测试的行,而不是只突出显示运行测试。

我不知道前面会出现什么,所以我想突出显示有运行测试的那一行。

​

代码语言:javascript
复制
//highlight words in the results
    $("p").html(function() {
        return $(this).html()
            .replace("Running test:", '<strong>Running test:</strong>')       
    })
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Running test: test1</p>
<p>ssfafasf</p>
<p>Running test: test2</p>
<p>gsgsddsh</p>

​

EN

回答 4

Stack Overflow用户

发布于 2018-03-12 20:37:24

您可以设置一个条件来验证内容是否包括和特定字符串...

​

代码语言:javascript
复制
//highlight words in the results
    $("p").html(function() {
      if($(this).html().includes("Running test:")){
             return '<strong>'+$(this).html()+'</strong>';
            
      }
      else  return $(this).html();
            
    })
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Running test: test1</p>
<p>ssfafasf</p>
<p>Running test: test2</p>
<p>gsgsddsh</p>

​

票数 0
EN

Stack Overflow用户

发布于 2018-03-12 20:39:20

使用class的解决方案,检查每个p元素是否包含Running test:,然后添加类。

​

代码语言:javascript
复制
$("p").each(function() {
  if ($(this).html().indexOf("Running test:") >= 0) {
  $(this).addClass('makeItBold');
  } else {
  $(this).removeClass('makeItBold');
  }
});
代码语言:javascript
复制
.makeItBold {
font-weight: bold;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Running test: test1</p>
<p>ssfafasf</p>
<p>Running test: test2</p>
<p>gsgsddsh</p>

​

票数 0
EN

Stack Overflow用户

发布于 2018-03-12 20:40:06

​

代码语言:javascript
复制
//highlight words in the results
    $("p").html(function() {
         if($(this).html().indexOf('Running test') !== -1)
         {
         $(this).addClass('highlight');
         }
                
    })
代码语言:javascript
复制
.highlight
{
font-weight : bold;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Running test: test1</p>
<p>ssfafasf</p>
<p>Running test: test2</p>
<p>gsgsddsh</p>

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49235178

复制
相关文章

相似问题

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