我正在尝试以绿色突出显示确定,黄色显示失败,红色显示错误。
我的结果是变量。如何搜索这些单词并为红色,绿色,黄色提供3个不同的类?
//highlight words in the results
var results = $('#results').html();
console.log(results);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="results">Fail blabla Error asda OK</span>
答案 0 :(得分:2)
解析html()
并在span
中包含子字符串,添加相应的class
。
$("#results").html(function() {
return $(this).html()
.replace("Fail", '<span class="fail">Fail</span>')
.replace("OK", '<span class="ok">OK</span>')
.replace("Error", '<span class="error">Error</span>');
})
.fail {
color: orange;
}
.error {
color: red;
}
.ok {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="results">Fail blabla Error asda OK</span>
答案 1 :(得分:0)
使用replace将所有关键字替换为//highlight words in the results
var results = $('#results').text();
results = results.replace(/OK/g, '<span style="background-color:green">OK</span>')
.replace(/Fail/g, '<span style="background-color:yellow">Fail</span>')
.replace(/Error/g, '<span style="background-color:red">Error</span>');
$('#results').html(results);
console.log(results);
。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="results">Fail blaErrorbla Error a OK sda OK</span>
<html>
<body>
<div id="avs">
<table id="av1"><tr><td>User 01</td><td>Paola</td></tr></table>
<table id="av2"><tr><td>User 02</td><td>Roger</td></tr></table>
<table id="av3"><tr><td>User 03</td><td>Jenny</td></tr></table>
</div>
</body>
</html>