首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用JS/jQuery在HTML表格中选择一个特定的单元格?

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

Stack Overflow用户
提问于 2019-04-21 01:22:52
回答 1查看 41关注 0票数 0

calendar month with 5 rows calendar month with 6 rows

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

我想使用JavaScript/jQuery选择HTML表中的单元格,这样就可以在条件语句中使用它的单元格文本来调整日历的大小。代码块需要如下所示:

代码语言:javascript
复制
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行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-21 01:41:45

您可以简单地在Jquery中使用:nth-child() Selector

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55775970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档