更改预标签

时间:2018-04-23 09:02:24

标签: jquery

我试图用特定的颜色显示一些单词:

PASSED - >绿色

失败 - >红色

到目前为止,我成功地只在同一个脚本中显示绿色或红色,但不能同时显示两者。谢谢你的帮助。

var text_change = '';

$(document).ready(function () {
  $("pre:contains('" + text_change + "')").each(function () {
    if ($text_change = 'PASSED') {
      var regex = new RegExp(text_change,'gi');
      $(this).html($(this).text().replace(regex, "<span class='green'>" + text_change + "</span>"));
    } else if ($text_change = 'FAILED') {
      var regex = new RegExp(text_change,'gi');
      $(this).html($(this).text().replace(regex, "<span class='red'>" + text_change + "</span>"));
    }
  }
});

1 个答案:

答案 0 :(得分:-1)

这就是你要找的东西:

$(document).ready(function () {
    $("pre").each(function(){
        $(this).html($(this).html().replace(/PASSED/g,"<span class='green'>PASSED</span>"));
        $(this).html($(this).html().replace(/FAILED/g,"<span class='red'>FAILED</span>"));
    });
});
span.green{
    color:green;
}
span.red{
    color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre>PASSED FAILED PASSED FAILED</pre>