<! 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> You can select all tables to be deleted. </title> <Style type = "text/css"> Body, table { Margin: 0; Padding: 0; Font-size: 14px; } Table, tr, th, td { Border: 1px solid # cdc; } Th { Background-color: green; Width: 100px; } . OddColor { Background-color: # ccc; } . EvenColor { Background-color: # fcf; } . OverColor { Background-color: # dff; } </Style> <Script type = "text/javascript"> // Define nested Arrays Var datas = [["Zhang San", 30, "Nanchang"], ["Li Si", 25, "Beijing"], ["Wang Wu", 20, "Zhengzhou"], ["Zhao 6", 19, "Wuhan"], ["Li Mo", 18, "Shenzhen"], ["Luo Cheng", 33, "Chongqing"], ["Wang Ping", 31, "Tianjin"], ["Liu Ping", 22, "Shanghai"], ["Yang Li", 17, "Shijiazhuang"], ["Guo Li", 30, "Guangzhou"]; // Dynamically create a table Function CreateMyTable (){ Var tblMain = document. getElementById ("tblMain "); Var rowsCount = tblMain. rows. length; Var addRow; Var addCol; Var detailInfos; For (var I = 0; I <datas. length; I ++ ){ AddRow = tblMain. insertRow (rowsCount-1); // Add control data rows to the penultimate Column AddCol = addRow. insertCell (-1 ); AddCol. innerHTML = "<input type = 'checkbox' name = 'item'/> "; AddCol. align = "center"; // center the control column DetailInfos = datas [I]; For (var j = 0; j <detailInfos. length; j ++ ){ AddCol = addRow. insertCell (-1 ); AddCol. innerHTML = detailInfos [j]; } AddCol = addRow. insertCell (-1 ); AddCol. innerHTML = "<input type = 'button 'id = 'btndel" + I + "'value = 'delete' onclick = 'btndel (this)'/> "; AddCol. align = "center"; // center the control column RowsCount ++; } TableColor (); } // Set grid interval color and highlight Var oldClassName; // remember the row background color Function TableColor (){ Var tblMain = document. getElementById ("tblMain "); Var rowNodes = tblMain. rows; For (var I = 1; I <rowNodes. length-1; I ++) {// remove the first and last rows If (I % 2 = 0 ){ RowNodes [I]. className = "evenColor "; } Else { RowNodes [I]. className = "oddColor "; } RowNodes [I]. onmouseover = function (){ OldClassName = this. className; This. className = "overColor "; } RowNodes [I]. onmouseout = function (){ This. className = oldClassName; } } } // Select all functions in the check box Function checkAll (){ Var currentCheckNode = event. srcElement; Var checkAllNodes = document. getElementsByName ("all "); // Remove the all selected check boxes that are not clicked. For (var I = 0; I <checkAllNodes. length; I ++ ){ If (currentCheckNode! = CheckAllNodes [I]) { CheckAllNodes [I]. checked = false; } } Var checkItemNodes = document. getElementsByName ("item "); For (var I = 0; I <checkItemNodes. length; I ++ ){ CheckItemNodes [I]. checked = currentCheckNode. checked; } } // Select a function Function btnCheckboxSel (index ){ Var checkItemNodes = document. getElementsByName ("item "); For (var I = 0; I <checkItemNodes. length; I ++ ){ If (index = 2 ){ CheckItemNodes [I]. checked =! CheckItemNodes [I]. checked; } Else { CheckItemNodes [I]. checked = index; } } } // Delete button function for each row Function btnDel (btn ){ Var tblMain = document. getElementById ("tblMain "); Var delRowNode = btn. parentNode. parentNode; Var sMsg = "are you sure you want to delete the name: [" + delRowNode. cells [1]. innerText + "], age: [" + DelRowNode. cells [2]. innerText + "], city: [" + DelRowNode. cells [3]. innerText + "] data? "; If (window. confirm (sMsg )){ TblMain. tBodies [0]. removeChild (delRowNode ); TableColor (); } } // Delete the selected Button Function Function btnDelSelectRow (){ Var arrDel = new Array (); Var pos = 0; Var itemNodes = document. getElementsByName ("item "); For (var I = 0; I <itemNodes. length; I ++ ){ If (itemNodes [I]. checked ){ ArrDel [pos] = itemNodes [I]. parentNode. parentNode; Pos ++; } } If (pos <= 0 ){ Return; } If (! Window. confirm ("Do you want to delete the selected data? ")) Return; Var tblMain = document. getElementById ("tblMain "); For (var I = 0; I <arrDel. length; I ++ ){ TblMain. tBodies [0]. removeChild (arrDel [I]); } } Window. onload = CreateMyTable; </Script> </Head> <Body> <Table id = "tblMain" cellspacing = "0" cellpadding = "10px" align = "center"> <Tbody> <Tr> <Th> <input type = "checkbox" name = "all" onclick = "checkAll ()"/> select all </th> <Th> name </th> <Th> age </th> <Th> city </th> <Th> operation </th> </Tr> <Tr> <Th> <input type = "checkbox" name = "all" onclick = "checkAll ()"/> select all </th> <Th colspan = "4"> <Input type = "button" value = "select all" onclick = "btnCheckboxSel (1)"/> <Input type = "button" value = "" onclick = "btnCheckboxSel (0)"/> <Input type = "button" value = "invert" onclick = "btnCheckboxSel (2)"/> <Input type = "button" value = "delete selected" onclick = "btnDelSelectRow ()"/> </Th> </Tr> </Tbody> </Table> </Body> </Html> |