我想加粗所有包含运行测试的行,而不是只突出显示运行测试。
我不知道前面会出现什么,所以我想突出显示有运行测试的那一行。
//highlight words in the results
$("p").html(function() {
return $(this).html()
.replace("Running test:", '<strong>Running test:</strong>')
})<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>
发布于 2018-03-12 20:37:24
您可以设置一个条件来验证内容是否包括和特定字符串...
//highlight words in the results
$("p").html(function() {
if($(this).html().includes("Running test:")){
return '<strong>'+$(this).html()+'</strong>';
}
else return $(this).html();
})<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>
发布于 2018-03-12 20:39:20
使用class的解决方案,检查每个p元素是否包含Running test:,然后添加类。
$("p").each(function() {
if ($(this).html().indexOf("Running test:") >= 0) {
$(this).addClass('makeItBold');
} else {
$(this).removeClass('makeItBold');
}
});.makeItBold {
font-weight: bold;
}<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>
发布于 2018-03-12 20:40:06
//highlight words in the results
$("p").html(function() {
if($(this).html().indexOf('Running test') !== -1)
{
$(this).addClass('highlight');
}
}).highlight
{
font-weight : bold;
}<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>
https://stackoverflow.com/questions/49235178
复制相似问题