Javascript allows you to select, reverse select, and delete tables.

Source: Internet
Author: User

Javascript allows you to select, reverse select, and delete tables.

This example describes how to select, reverse select, and delete a table in javascript. Share it with you for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

<! 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>

I hope this article will help you design javascript programs.

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.