我有简单的html表,如果td标签包含单词“hour”和“day”,我想将颜色设置为红色。我做了一个声明:
$("td:contains('hour')").css("color", "red");
我想要包含“day”这个词,我接下来尝试了:
$("td:contains('hour') && td:contains('day')").css("color", "red");
if($(“td:contains('hour')”)&&($“td:contains('day')”)) { css(“color”,“red”); }
我怎么做?
答案 0 :(得分:1)
尝试这样。为了匹配多个单词,您再次使用contains
。
$("td:contains('hour'):contains('day')").css("color", "red");
实施例
$("p:contains('is'):contains('name')").css("background-color", "red");

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>My name is Donald.</p><!--it will matched contains both `is` and `name`-->
<p>I live in Duckburg.</p><!--not matched -->
<p>My best friend is Mickey.</p><!--not matched contains only `is`-->
&#13;
答案 1 :(得分:0)
您需要在找到第一个匹配项后进一步细化选择器,并了解测试的区分大小写的特性(仅找到小写匹配)。
var escalation = escalationMapper.GetSingle(trackingGroupCode);
&#13;
$("td:contains('hour'):contains('day')").css("color", "red");
&#13;
table, td { border:1px solid black; }
&#13;
对于不区分大小写的搜索:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>Hour</td>
<td>Day</td>
<td>hour</td>
<td>day</td>
<td>None</td>
<td>hour and day</td>
<td>Hour and Day</td>
</tr>
</table>
&#13;
var theCells = $("td");
// Loop through the cells and check lower-case text against lower-case value
theCells.each(function(){
if(this.textContent.toLowerCase().indexOf("hour") > -1 &&
this.textContent.toLowerCase().indexOf("day") > -1){
this.style.color = "red";
}
});
&#13;
table, td { border:1px solid black; }
&#13;