如何使用JS / jQuery在HTML表中选择一个特定的单元格?

时间:2019-04-20 17:22:52

标签: javascript jquery html-table

calendar month with 5 rows calendar month with 6 rows

我正在使用HTML(表格)和JavaScript创建动态日历;我想根据特定月份是否需要5或6行来显示整个月份来调整表格单元格的高度和宽度。

我想使用JavaScript / jQuery在HTML表中选择单元格,因此我可以在条件语句中使用其单元格文本来调整日历的大小。该代码块需要看起来像这样:

var td = $(td) // selects all <td> elements

var tdCheck6 = $(...) // selects cell text of cell in 1st row and 6th column
var tdCheck7 = $(...) // selects cell text of cell in 1st row and 7th column 

/* using Monday as the first day of the week, if (Saturday is the first day 
of one particular month AND the month has 31 days) OR (Sunday is the first day 
of the month AND the month has more than 29 days: that month will need 6 rows) */

if ((tdCheck6 == "1" && daysInMonth == 31) || (tdCheck7 == "1" && daysInMonth > 29)) {
     td.style.cssText = "height: 100px; width: 14.29%;";
} 
// use default cell dimensions for 5 rows
else {
     td.style.cssText = "height: 120px; width: 14.29%;"; 
}  

通过检查这些条件,日历将根据月份重新调整日历视口以适合5或6行。

1 个答案:

答案 0 :(得分:0)

您只需在Jquery中使用:nth-child() Selector

示例:$("td:nth-child(1)").text()

有关更多详细信息 https://api.jquery.com/nth-child-selector/