简体   繁体   English

Javascript表/行选择

[英]Javascript table / row select

I am asking to help me in following issue: I have a table created in Javascript by 我要求在以下问题上帮助我:我有一个用Java创建的表

document.createElement('table')
document.createElement('tr')
document.createElement('td')

The table is a web-calendar / organizer. 该表是一个网络日历/组织者。 So, I need the top row to display both dates and days in the cells. 因此,我需要第一行在单元格中显示日期和日期。 For now I have a code which displays only dates there. 现在,我有一个仅在其中显示日期的代码。 But how can I refer to the cells to put there names of days? 但是,如何引用放置日期名称的单元格呢?

I must say that ONLY native Javascript is available to use (no JS libraries or whatever)! 我必须说, 只有本地Javascript可以使用(没有JS库或其他任何东西)! If the table had been created by HTML code I'd have referred to its top row manually via data attribute. 如果该表是通过HTML代码创建的,则可以通过data属性手动引用其顶行。

But I've got no idea about what should I do in the current situation. 但是我不知道在当前情况下该怎么办。

Any help will be appreciated! 任何帮助将不胜感激!

Try this: 尝试这个:

var currentTd = document.createElement('td');

Now you've got a variable you can use and you give it an id: 现在您有了一个可以使用的变量,并为其指定了一个id:

currentTd.id = 'td1';

Later you can use getElementById('td1') and put in the days of the week. 稍后,您可以使用getElementById('td1')并输入星期几。

or you can refer to it now before sticking it into the dom: 或者您也可以在将其放入dom之前先进行参考:

currentTd.innerHTML = 'Saturday 11th January';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM