Dynamically create tables and delete Columns

Source: Internet
Author: User

 

Java code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> untitled document </title>

<Link rel = "stylesheet" href = "tab.css"/>

<Script type = "text/javascript" src = "../Day26/doctool. js"> </script>

<Script type = "text/javascript">

Function createTab1 ()

{

Var tabNode = doc. createElement ("table ");

Var tbdNode = doc. createElement ("tbody ");

Var trNode = doc. createElement ("tr ");

Var tdNode = doc. createElement ("td ");

Var textNode = doc. createTextNode ("unit 1 ");

TdNode. appendChild (textNode );

TrNode. appendChild (tdNode );

TbdNode. appendChild (trNode );

TabNode. appendChild (tbdNode );

ByTag ("div") [0]. appendChild (tabNode );

}

 

Function createTab ()

{

Var tabNode = doc. createElement ("table ");

// TabNode. id = "tabid ";

TabNode. setAttribute ("id", "tabid ");

Var row = byName ("rownum") [0]. value;

Var col = byName ("colnum") [0]. value;

For (var x = 1; x <= row; x ++)

{

Var trNode = tabNode. insertRow ();

For (var y = 1; y <= col; y ++)

{

Var tdNode = trNode. insertCell ();

TdNode. innerHTML = x + "..." + y;

}

}

ByTag ("div") [0]. appendChild (tabNode );

Event. srcElement. disabled = true;

}

 

Function delRow ()

{

Var tabNode = byId ("tabid ");

If (tabNode = null)

{

Alert ("table does not exist ");

Return;

}

Var rownum = byName ("delrow") [0]. value;

If (rownum> 0 & rownum <= tabNode. rows. length)

TabNode. deleteRow (rownum-1 );

Else

{

Alert ("the row to be deleted does not exist, learning count is very important ");

}

}

 

Function delCol ()

{

Var tabNode = byId ("tabid ");

If (tabNode = null)

{

Alert ("table does not exist ");

Return;

}

Var colnum = byName ("delcol") [0]. value;

If (colnum> 0 & colnum <= tabNode. rows [0]. cells. length)

{

For (var x = 0; x <tabNode. rows. length; x ++)

{

TabNode. rows [x]. deleteCell (colnum-1 );

}

}

Else

{

Alert ("the deleted column does not exist ");

}

}

 

</Script>

 

</Head>

 

 

<Body>

<! --

You can use the buttons on the page to dynamically create a table.

-->

Row: <input type = "text" name = "rownum"/> <br/>

Column: <input type = "text" name = "colnum"/> <br/>

 

<Input type = "button" value = "create table" onclick = "createTab ()"/> <br/>

 

<Input type = "text" name = "delrow"/>

<Input type = "button" value = "Delete row" onclick = "delRow ()"/>

<Br/>

<Input type = "text" name = "delcol"/>

<Input type = "button" value = "delete column" onclick = "delCol ()"/>

<Br/>

<Br/>

 

<Div>

</Div>

</Body>

</Html>

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.