jquery代码,用于设置特定字符串的颜色

时间:2014-03-10 05:01:47

标签: jquery

我有一个这样的字符串:

var report = 'Verifying Wireless Interface is present and state is Disconnected:PASSED<br>Verify Profile not present on the Client:PASSED<br>Add Profile to the Client:PASSED<br>Verify Profile Added Present on the Client:PASSED<br>Connecting to Access Point:PASSED<br>Verify the State is Connected:PASSED<br>Disconnecting from Access Point:PASSED<br>Verify the State is Disconnected:PASSED<br>ReConnecting to Access Point:PASSED<br>Verify the State is Connected:PASSED<br>Verify Ping to the Gateway (Access point):PASSED<br>Verify Signal Strength Greater than 75%:PASSED<br>Verify Round Trip Time not greater than 30 millieseconds:PASSED<br>Disconnecting from Access Point:PASSED<br>Verify the State is Disconnected:PASSED<br>Delete Profile to the Client:PASSED<br>Verify Profile Not Present on the Client:Failed<br>';

由此,我必须为PASSED字符串设置绿色,为Failed字符串设置红色。怎么可能......

3 个答案:

答案 0 :(得分:7)

您需要将这些单词包装在另一个元素中

report = report.replace(/:PASSED/g, ':<span class="passed">PASSED</span>').replace(/:Failed/g, ':<span class="failed">Failed</span>');

然后

.passed {
    color:green
}
.failed {
    color:red;
}

演示:Fiddle

答案 1 :(得分:2)

小提琴:http://jsfiddle.net/sajith/w3qhb/

<强> HTML

<div id="report"></div>

<强>的Javascript

var report =  'Verifying Wireless Interface is present and state is Disconnected:PASSED<br>Verify Profile not present on the Client:PASSED<br>Add Profile to the Client:PASSED<br>Verify Profile Added Present on the Client:PASSED<br>Connecting to Access Point:PASSED<br>Verify the State is Connected:PASSED<br>Disconnecting from Access Point:PASSED<br>Verify the State is Disconnected:PASSED<br>ReConnecting to Access Point:PASSED<br>Verify the State is Connected:PASSED<br>Verify Ping to the Gateway (Access point):PASSED<br>Verify Signal Strength Greater than 75%:PASSED<br>Verify Round Trip Time not greater than 30 millieseconds:PASSED<br>Disconnecting from Access Point:PASSED<br>Verify the State is Disconnected:PASSED<br>Delete Profile to the Client:PASSED<br>Verify Profile Not Present on the Client:Failed<br>';
report = report.replace(/PASSED/ig, '<span class="p">PASSED</span>');
report = report.replace(/FAILED/ig, '<span class="f">Failed</span>');
$("#report").html(report);

<强> CSS

#report span.p {color:red;}
#report span.f {color:green;}

答案 2 :(得分:1)

  <script>
var report =  'Verifying Wireless Interface is present and state is     Disconnected:PASSED<br>Verify Profile not present on the Client:PASSED<br>Add Profile to the Client:PASSED<br>Verify Profile Added Present on the Client:PASSED<br>Connecting to Access Point:PASSED<br>Verify the State is Connected:PASSED<br>Disconnecting from Access Point:PASSED<br>Verify the State is Disconnected:PASSED<br>ReConnecting to Access Point:PASSED<br>Verify the State is Connected:PASSED<br>Verify Ping to the Gateway (Access point):PASSED<br>Verify Signal Strength Greater than 75%:PASSED<br>Verify Round Trip Time not greater than 30 millieseconds:PASSED<br>Disconnecting from Access Point:PASSED<br>Verify the State is Disconnected:PASSED<br>Delete Profile to the Client:PASSED<br>Verify Profile Not Present on the Client:Failed<br>'


    var valuePassed = report.replace(/PASSED/g, '<span class="pass">PASSED</span>'); 
    var valueFianal = valuePassed.replace(/Failed/g, '<span class="fail">Failed</span>'); 
    alert(valueFianal);
</script>

使用失败,在css中传递类样式