How to obtain predefined Table What about the row number and column number? Easy to understand Table The usage of several attribute values. Table The total number of rows can be marked by Tr " Yes, Table The number of Columns cannot be obtained directly. Rows By marking Th " And" TD .
In order to be more intuitive, the following example shows how to obtain the row number and column number. ClickIDIs'Mytable'Returns the row number and column number of any unit in the table! ExampleCodeAs follows:
FunctionGetrowandcolumn ()
{
If (! Document. getelementsbytagname |! Document. createtextnode) return ;
var rows =< strong> document. getelementbyid ( 'mytable' ). getelementsbytagname ( 'tr' );
VaRCols;
For(I = 0; I <rows. length; I ++)
{
Rows [I].Onclick=Function()
{
alert ( " line :" + eval ( This . rowindex + 1);
}
If(I = 0)
{
Colth =Rows [I]. getelementsbytagname ('Th');
Alert (colth. Length );
For(K = 0; k <colth. length; k ++)
{
Colth [K]. onclick =Function()
{
alert ( " column :" + eval ( This . cellindex + 1);
}
}
}
Else
{
Cols = rows [I]. getelementsbytagname ( 'td ' ) ;
For(J = 0; j <cols. length; j ++)
{
Cols [J]. onclick =Function()
{
alert ( " column :" + eval ( This . cellindex + 1);
}
}
}
}
}
(Note: the key code has been hacked)