我有一个使用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('\"\')")
有人能指出我正确的方向吗?
答案 0 :(得分:0)
检查以下小提琴
$('#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;
答案 1 :(得分:0)
$('#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');
}
});
答案 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');
};
}
});