如果文字"关闭"我通过CSS更改背景。找到了。工作正常。
$('td:contains("Off")').filter(function() {
return $(this).text() == "Off";
}).css({"background":"#667881", "color":"white"});
有没有办法修改上面的jQuery .filter函数,只测试前3个字符(仅捕获' Off')?一些文本字段包含更多:
"关闭更多单词"
通过上面的代码,这些目标并不是针对性的,即使它们包含“关闭”
但是,我不希望这样的目标成为目标:"更多的话语关闭"
答案 0 :(得分:2)
您可以将 match()
与正则表达式 ^Off
一起使用,它只匹配字符串开头Off
$('td').filter(function() {
return $(this).text().match(/^Off/);
}).css({
"background": "#667881",
"color": "white"
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tr>
<td>Off
</td>
<td>omff
</td>
<td>off
</td>
<td>coff
</td>
</tr>
</table>
答案 1 :(得分:2)
使用match():
<div class="form-group">
@Html.LabelFor(model => model.DriverID, "Driver Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.DriverID, Model.DriverList, "-Please select-", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DriverID, "", new { @class = "text-danger" })
</div>
</div>
$('td:contains("Off")').filter(function() {
return $(this).text().match("^Off");
}).css({"background":"#667881", "color":"white"});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tr>
<td>Off some more words</td>
</tr>
<tr>
<td>some more words Off</td>
</tr>
<tr>
<td>some more Off words Off</td>
</tr>
</table>
仅在文字以^