Use JavaScript to obtain table row numbers and column numbers (with source code)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.