嘗試為第二列和第三列中包含years<= 2004 的表格行著色應該具有值"Ne"。我現在得到的是僅包含第三列“Ne”中的值的彩色行,并且條件 years<=2005 被忽略。有什么方法可以使用&&撰寫條件,或者我的語法不好?
$(function(){
$("#randa").click(function(){
$("#lentele td:nth-child(2)").each(function()
{
if (parseInt($(this).text()) <=2005)
{
$("#lentele td:nth-child(3)").each(function()
{
if ($(this).text() == "Ne")
{
$(this).parent("tr").addClass('spalvinti');
}
}
)
}
})
})
});

uj5u.com熱心網友回復:
當您找到帶有 text 的適用單元格時Ne,它需要與帶有年份的匹配單元格在同一行。有問題的代碼是找到所有的單元格Ne。
我將逐行處理如下:
$("#randa").click(function(){
$("#lentele tr").each(function() {
if (parseInt($(this).children('td:nth-child(2)').text()) <= 2005
&& $(this).children('td:nth-child(3)').text() === 'Ne') {
$(this).addClass('spalvinti');
}
});
});
table { border: 1px solid #eee; }
tr.spalvinti { background-color: green; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="randa">
<table id="lentele">
<tbody>
<tr>
<td>1</td><td>2010</td><td>Taip</td>
</tr>
<tr>
<td>2</td><td>1999</td><td>Ne</td>
</tr>
<tr>
<td>3</td><td>2001</td><td>Ne</td>
</tr>
<tr>
<td>4</td><td>2004</td><td>Ne</td>
</tr>
<tr>
<td>5</td><td>2008</td><td>Ne</td>
</tr>
</tbody>
</table>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/360998.html
標籤:javascript html 查询
