在jQuery循环中选择多个表类

时间:2016-08-05 14:01:54

标签: jquery momentjs

我有一个使用jQuery&的表。 moment.js从列中获取时间,检查时间是否大于X分钟,如果是,请突出显示单元格。

例如:https://jsfiddle.net/kjscq4ra/

<table id="myTable">
    <tr>
        <th>Number</th>
        <th>Name</th>
        <th>Time</th>
    </tr>
    <tr>
        <td>123</td>
        <td class="person">Jack "Smith"</td>
        <td class="time">8/03/2016 8:35:00 AM</td>
    </tr>
    <tr>
        <td>246</td>
        <td class="person">John "A" Doe</td>
        <td class="time">8/03/2016 7:05:30 AM</td>
    </tr>
    <tr>
        <td>369</td>
        <td class="person">Jane</td>
        <td class="time">8/03/2016 7:15:25 AM</td>
    </tr>
</table>
$('#myTable td.time').each(function() {
    var cellText = $(this).text();
    var a = moment();
    var b = moment(cellText, "MM/DD/YYYY hh:mm:ss");
    var difference = a.diff(b, 'minutes');
    if (difference >= 60) {
        $(this).css('background-color', '#FFBABA');
    };
});

我想在我的if声明中添加一个条件,以便检查&#34; person&#34;在突出显示&#34; time&#34;之前的某些文字的列柱。例如,只突出显示&#34; time&#34;列如果&#34; person&#34; td在名称中包含撇号:

$("#myTable td.person:contains('\"\')")

有人能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:0)

检查以下小提琴

&#13;
&#13;
$('#myTable td.time').each(function() {
  if ($(this).siblings("td.person").html().indexOf("\"") > -1) {
		var cellText = $(this).text();
    var a = moment();
    var b = moment(cellText, "MM/DD/YYYY hh:mm:ss");
    var difference = a.diff(b, 'minutes');
    if (difference >= 60) {
      $(this).css('background-color', '#FFBABA');
    };
  }
});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable">
  <tr>
    <th>Number</th>
    <th>Name</th>
    <th>Time</th>
  </tr>
  <tr>
    <td>123</td>
    <td class="person">Jack "Smith"</td>
    <td class="time">8/03/2016 8:35:00 AM</td>
  </tr>
  <tr>
    <td>246</td>
    <td class="person">John "A" Doe</td>
    <td class="time">8/03/2016 7:05:30 AM</td>
  </tr>
  <tr>
    <td>369</td>
    <td class="person">Jane</td>
    <td class="time">8/03/2016 7:15:25 AM</td>
  </tr>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

See jQuery's prev function

$('#myTable td.time').each(function() {
  var $timeCell = $(this),
    cellText = $timeCell.text(),
    personText = $timeCell.prev('.person').text(),
    a = moment(),
    b = moment(cellText, "MM/DD/YYYY hh:mm:ss"),
    difference = a.diff(b, 'minutes');

  if (difference >= 60 && personText.indexOf('"') > -1) {
    $timeCell.css('background-color', '#FFBABA');
  }
});

https://jsfiddle.net/kjscq4ra/4/

答案 2 :(得分:0)

可以尝试一下。您可以使用 $。prev / $ .siblings 来获取元素并检查其值

$('#myTable td.time').each(function() {
  if( $(this).prev('td.person').text().match(/\"|\'/)) 
 {
  var cellText = $(this).text();
  var a = moment();
  var b = moment(cellText, "MM/DD/YYYY hh:mm:ss");
  var difference = a.diff(b, 'minutes');
  if (difference >= 60) {
    $(this).css('background-color', '#FFBABA');
  };
 }
});