将css添加到动态添加的Tablerow中

时间:2016-10-04 14:34:35

标签: jquery html css jquery-mobile

我正在使用jquery mobile创建一个网站,在该网站上我得到了一个如下表:
FIDDLE



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.css">
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<table data-role="table" class="ui-responsive ui-shadow gk-decorate" id="testTable" is="jqm-table">
  <thead>
    <tr>
      <th data-priority="1">Animal</th>
      <th data-priority="1">Sports</th>
      <th data-priority="1">Names</th>
      <th data-priority="1">Fruits</th>
      <th data-priority="1">Status</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Cat</th>
      <td>Football</td>
      <td>Anna</td>
      <td>Apple</td>
      <td></td>
    </tr>
    <tr>
      <th>Dog</th>
      <td>Baseball</td>
      <td>James</td>
      <td>Banana</td>
      <td>Checked</td>
    </tr>
    <tr>
      <th>Shark</th>
      <td>Hockey</td>
      <td>David</td>
      <td>Orange</td>
      <td>Checked</td>
    </tr>
    <tr>
      <th>Ape</th>
      <td>PingPong</td>
      <td>Locke</td>
      <td>Mango</td>
      <td></td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

我的问题是:我可以以某种方式将css类或css添加到"Checked"内的字符串Status Coloumn的行中。我试过这样的东西:

var qTable = document.getElementById("testTable");
var row = qTable.tBodies[0].insertRow();
row.addClass() //error does not support addClass
row.css({..})//error does not support css..
  

注意:表格内容会动态添加

3 个答案:

答案 0 :(得分:1)

试试这个,

JS

$("#testTable td").each(function(){
    if($(this).text() =='StatusChecked'){
   $(this).parent().addClass('checked');
   }
});

CSS(仅用于测试样式)

.checked{
  background:green;
}

更新小提琴 -

https://jsfiddle.net/guruling/erc3ujzw/

答案 1 :(得分:1)

使用@ObjectRepository(src="/login.csv") public class LoginPage{ private Map<String, WebElement> elements; public void login(){ elements.get("username").sendKeys(''); elements.get("password").sendKeys(''); elements.get("signin").click(); } } 伪选择器使用您的标记过滤掉contains:,然后获取其父节点(<td> s)

<tr>

https://jsfiddle.net/rttmq4we/

答案 2 :(得分:1)

尝试:

$("#testTable tr").each(function(){
  var tds  = $(this).find("td");
  var statusCell = $(tds[3]);
  var statusCellTxt = statusCell.text();
  if (statusCellTxt.indexOf('Checked') !== -1){
    $(statusCell).css('border', '1px solid red');
  }
});

请参阅小提琴HERE