JS operation table! JS table row count
Although table is rarely used in the foreground at the moment, table, such as forms or other lists, is more convenient to use table than Div, to use table, add <a href = "http://www.5i2i.com/<a % 20 href =" target = _ blank> JavaScript/'target = '_ blank'> js to make it a fact. effect, for example, delete a line, add a line, to be, it is necessary to familiarize yourself with the <a href = "http://www.5i2i.com/<a % 20 href =" target = _ blank> JavaScript /'
Target = '_ blank'> JS operations on table,
First, we need to obtain the table object. There are many methods,
Obtained by ID, table = Document. getelementbyid ()
Obtained through class, table = Document. getelementsbyclass ()
Obtained Through tabname, table = Document. getelementsbytagname ()
After obtaining the table object, we may want to know how many rows the table object has,
You can use table. Rows. length to obtain the number of rows. Table. Rows returns the set of rows of the current table object. You can use ROW = table. Rows [I] to obtain the row I object of the table object. For the row object, it has an attribute, that is, row. rowindex returns I,
Then, insert rows. There are two functions for you to use,
Insertrow (INDEX) and insertcell (INDEX); when you see the function name, you can understand what the function is. Yes, you can call the insertrow function through the table object, you can insert a row in the table object and return the newly created row object. The index parameter is obtained, indicating the number of rows inserted, starting from 0, however, the index cannot be less than 0 or greater than the existing number of rows in the Table. Otherwise, an error is returned. Similarly, you can use a row object to call the insertcell function to insert a unit cell in the row object and return a entry cell object. The index parameter is also obtained by using the length of the cells set of the row object, row. cells. length.
Table id = "tab">
Boil the first line boil the first column
The second column in the first row
The first column in the second row
The second row and the second column
The third row is the first column
Row 3 and column 2
Click here and I
The above example can be run in ff and IE, and rows can be correctly added.
What if you want to delete the row?
I did this by getting the parent node and deleting the child node.
First obtain the row object TR, and then obtain the parent node of the tr object. Par = tr. parentnode (here, why use the parent node of TR to obtain the parent node, instead of directly obtaining the parent node through other direct methods, because ff and IE are different here, in IE, tables are TR, while in FF tables there are thead and tbody, while in tbody, there are tr. If you get it directly, you need to determine if you get the parent node through TR, can be used directly), and then through the Par. removechild (TR) to delete rows. The details are not examples.
Http://apps.hi.baidu.com/share/detail/43618150