在jQuery中匹配字符串

时间:2016-09-13 11:02:17

标签: jquery string

我有这张桌子

<table>
  <tr>
    <td>Name</td>
    <td>John Stevenson</td>
  </tr>
  <tr>
    <td>Name</td>
    <td>John Stevenson</td>
  </tr>
  <tr>
    <td>Name</td>
    <td>Jack Stevenson</td>
  </tr>
</table>

我怎样才能找到杰克史蒂文森&#34;如果为true,请将此td颜色设置为红色。

我的jQuery代码不起作用:

$("table").find("td").each(function () {
  var hiImJohn = $("td:contains('John Stevenson')");
  var td = $(this).text();

  if ( td.indexOf("John Stevenson")  > 0 ) {
      hiImJohn.css({color: "red"});
  }
});

2 个答案:

答案 0 :(得分:3)

尝试以下

$("table").find("td:contains('John Stevenson')").each(function () {
  $(this).css({color: "red"});
});

供参考 - http://plnkr.co/edit/q45cGWpW8ql7FfoNaKDj?p=preview

修改

请注意,也不需要每个循环。更新后的代码如下

$("table").find("td:contains('John Stevenson')").css({color: "red"});

答案 1 :(得分:1)

试试这个

 </select>

 <select class="form-control" ng-model="join.targetColumn" 
 ng-options = "tcolumn.columnName for tcolumn in columns">
</select>

<a ng-click="addoperation(join)">add</a>
<br>
<table class="table bordered">
<tr>
<th>Source Tabel</th>
<th>Source Column</th>
<th>Target Tabel</th>
<th>Target Column</th>
</tr>
  <tr>{{operationInfo}}</tr>
  <tr ng-repeat = "info in operationInfo">
   <td>{{info.sourceTable.label}}</td>
   <td>{{info.sourceColumn.columnName}}</td>
   <td>{{info.targetTable.label}}</td>
   <td>{{info.targetColumn.columnName}}</td>
</tr>