Operations of JavaScript DOM on tables

Source: Internet
Author: User

Measure the test taker's knowledge about table attributes and subtags.

  • Summary attribute: used to summarize the content of the entire table. It is very important for robots in search engines to record information.
  • Bordercolor attribute: used to set the border color of a table. However, different browsers display different effects. (The bordercolor attribute is not recommended, but the border attribute of the CSS style sheet is recommended)
  • Cellspacing attribute: used to set the spacing between cells in a table. (We recommend that you use the border-collapse attribute of the CSS style sheet)
  • <Caption> MARK: indicates the title of a table, which can appear anywhere between <table>. It is very important for robots in search engines to record information.
  • <Th> MARK: the name of the row or column in the table.
  • <Th> the scope attribute of the tag: Used for the branch name and column name. For example, <th scope = 'row'> or <th scope = 'col'>

<Table> also contains the <thead>, <tbody>, and <tfoot> tags. They indicate the table header, table body, and table footer respectively. When printing the content of a webpage, if the table is large and one page cannot be printed, <thead> and <tfoot> will appear on each page. (Note: It is invalid in IE but valid in Firefox)/p> A table example

<Html>  

The table is shown as follows:

Report 2005-2008
2005 2006 2007 2008
Appropriation 11,980 12,650 9,700 10,600
Donation 4,780 4,989 6,700 6,590
Investment 8,000 8,100 8,760 8,490
Fundraising 3,200 3,120 3,700 4,210
2008 Statistics

Note: IE6 only supports the <a> flag: hover pseudo class. Other labels are not supported! And the <a> marked pseudo classes are ordered: a: link-> a: visited-> a: hover-> a: active

Use DOM methods and attributes to perform dynamic table operations

Use DOM to dynamically Add a row:

<Script language = "javascript"> window. onload = function () {// insert a line var oTr = document. getElementById ("mytable "). insertRow (2); var aText = new Array (); aText [0] = document. createTextNode ("cell1 content"); aText [1] = document. createTextNode ("cell2 content"); aText [2] = document. createTextNode ("cell3 content"); aText [3] = document. createTextNode ("cell4 content"); aText [4] = document. createTextNode ("cell5 content"); for (var I = 0; I <aText. length; I ++) {var oTd = oTr. insertCell (I); oTd. appendChild (aText [I]) ;}</script>

Use DOM to modify cell content:

<Script language = "javascript"> window. onload = function () {var oTable = document. getElementById ("mytable"); // modify the cell content oTable. rows [3]. cells [4]. innerHTML = "changed content" ;}</script>

Use DOM to delete a cell or row:

<Script language = "javascript"> window. onload = function () {var oTable = document. getElementById ("mytable"); // delete a row, and the following row number is automatically added to oTable. deleteRow (2); // delete a cell, and oTable is automatically added later. rows [2]. deleteCell (1) ;}</script>

Use DOM to dynamically Add a column and delete a row dynamically:

<Script language = "javascript"> function myDelete () {var oTable = document. getElementById ("mytable"); // Delete this row. parentNode. parentNode. parentNode. removeChild (this. parentNode. parentNode);} window. onload = function () {var oTable = document. getElementById ("mytable"); var oTd; // dynamically Add the delete link for (var I = 1; I <oTable. rows. length; I ++) {oTd = oTable. rows [I]. insertCell (5); oTd. innerHTML = "<a href = '#'> delete </a>"; oTd. firstChild. onclick = myDelete; // Add deletion event }}</script>

Use DOM to dynamically delete a column:

<Script language = "javascript"> function deleteColumn (oTable, iNum) {// User-Defined delete column function, that is, each row deletes the corresponding cell for (var I = 0; I <oTable. rows. length; I ++) oTable. rows [I]. deleteCell (iNum);} window. onload = function () {var oTable = document. getElementById ("mytable"); deleteColumn (oTable, 2); // parameter 2: column number to be deleted} </script>

Related Article

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.