您的位置:时时app平台注册网站 > 彩世界网址 > javascript贯彻可全选、反选及删除表格的不二等秘

javascript贯彻可全选、反选及删除表格的不二等秘

2019-12-06 10:56

仰望本文所述对咱们的javascript程序设计有所扶持。

javascript兑现表格增加和删除改操作实例详细解释

 本文实例陈说了javascript完毕表格增加和删除改操作的艺术。分享给大家供大家参照他事他说加以考查。具体落到实处况势如下:

?

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

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

<!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>javascript 表格增删改</title>

<script type="text/javascript">

var _OTable_ = null;

var _oTbody_ = null;

var _arrSelect_ = new Array;

var _oTempValue_=new Object;

_oTempValue_["$updateIndex"]=-1;

var _TheadName_=new Array("姓名","性别","年龄","籍贯","删除否");

var CELLS_COUNT=_TheadName_.length-1;

String.prototype.trim=function()

{

return this.replace(/(^s*)(s*$)/g, '');

}

window.onload = function()

{

var $oAdd = document.getElementById("btnAdd");

$oAdd.onclick = function()

{

var _oCol1_ = document.getElementById("Col1");

var _oCol2_ = document.getElementById("Col2");

var _oCol3_ = document.getElementById("Col3");

var _oCol4_ = document.getElementById("Col4");

if (!_OTable_) //如果不存在表则新建

{

_OTable_ = document.createElement("table");

_OTable_.setAttribute("border", "1");

_OTable_.setAttribute("width", "800px");

var _Thead_=_OTable_.createTHead();

var _TRow_=_Thead_.insertRow(0);

for(var _headindex_=0;_headindex_<CELLS_COUNT 1;_headindex_ )

{

var _tTh=_TRow_.insertCell(_headindex_);

_tTh.appendChild(document.createTextNode(_TheadName_[_headindex_]));

}

_oTbody_ = document.createElement("tbody");

_OTable_.appendChild(_oTbody_);

document.getElementById("TableData").appendChild(_OTable_);

}

if(!_oCol1_.value.trim()){alert("姓名必须填写!"); return;}

//添加一行

var _oRow_ = _oTbody_.insertRow(-1);

//添加5列,四列值,一列选择

var _oCell1_ = _oRow_.insertCell(-1);

_oCell1_.appendChild(document.createTextNode(_oCol1_.value));

var _oCell2_ = _oRow_.insertCell(-1);

_oCell2_.appendChild(document.createTextNode(_oCol2_.value));

var _oCell3_ = _oRow_.insertCell(-1);

_oCell3_.appendChild(document.createTextNode(_oCol3_.value));

var _oCell4_ = _oRow_.insertCell(-1);

_oCell4_.appendChild(document.createTextNode(_oCol4_.value));

_oCol1_.value = "";

_oCol2_.value = "";

_oCol3_.value = "";

_oCol4_.value = "";

//选择

var _oCell5_ = _oRow_.insertCell(4);

_oCell5_.setAttribute("style", "width:50px;");

var _oCheckBox_ = document.createElement("input");

_oCheckBox_.setAttribute("type", "checkbox");

_oCell5_.appendChild(_oCheckBox_);

_oCheckBox_.onclick = function()

{

if (_oCheckBox_.checked)

{

var _rowIndex_ = _oCheckBox_.parentNode.parentNode.rowIndex-1;

_arrSelect_.push(_rowIndex_);

}

}

_oRow_.ondblclick = function()

{

var _oPreUpdateIndex_=_oTempValue_["$updateIndex"];

var _oPreTempRow_=null;

if (parseInt(_oPreUpdateIndex_) != -1) //原先选定行重置

{

if (!_OTable_ || !_oTbody_) return;

_oPreTempRow_= _oTbody_.rows[parseInt(_oPreUpdateIndex_)];

var _cancelornot_=false;

for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_ )

{

var $attributeNode_=_oPreTempRow_.cells[_cellindex_].firstChild;

var $nodedata_=document.all?$attributeNode_.getAttribute("value"):$attributeNode_.value;

if($nodedata_!=_oTempValue_["$" _cellindex_])//与原值比较

{

_cancelornot_=confirm("你之前的内容作了修改,保存修改吗?");

break;

}

}

if(_cancelornot_)

{

//避免前次提交为空

var _firstNode_=_oPreTempRow_.cells[0].firstChild;

var $firstnodedata_=_firstNode_.getAttribute("value");

if(!$firstnodedata_||!$firstnodedata_.trim()){alert("姓名不能为空,请重新编辑!"); _firstNode_.focus(); return;};

for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_ )

{

var _oldNode_=_oPreTempRow_.cells[_cellindex_].firstChild;

var $nodedata_=document.all?_oldNode_.getAttribute("value"):_oldNode_.value;

var _textnode_= document.createTextNode($nodedata_);

_oPreTempRow_.cells[_cellindex_].replaceChild(_textnode_,_oldNode_);

delete _oTempValue_["$" _cellindex_];

}

}

else

{

for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_ )

{

var _oldNode_=_oPreTempRow_.cells[_cellindex_].firstChild;

var _textnode_= document.createTextNode(_oTempValue_["$" _cellindex_]);

_oPreTempRow_.cells[_cellindex_].replaceChild(_textnode_,_oldNode_);

delete _oTempValue_["$" _cellindex_];

}

}

}

_oTempValue_["$updateIndex"] = this.rowIndex-1;

//单元格中只有一个文本节点

for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_ )

{

var _textbox_= document.createElement("input");

_textbox_.setAttribute("type", "text");

var _preNode_=this.cells[_cellindex_].firstChild;

_oTempValue_["$" _cellindex_]=_preNode_.nodeValue; //记录原先的值

_textbox_.setAttribute("value",_preNode_.nodeValue);

this.cells[_cellindex_].replaceChild(_textbox_ ,_preNode_);

}

};

};

//删除

var $oDelete = document.getElementById("btnDelete");

$oDelete.onclick = function()

{

if (_arrSelect_.length == 0) { alert("您还没有选择要删除的行."); return; }

 

if (_OTable_ && _oTbody_)

{

var _confirmMsg_ = "你确定要删除名字是如下:n";

for (var index = 0, iLen = _arrSelect_.length; index < iLen; index )

{

var _deletName_ = _oTbody_.rows[parseInt(_arrSelect_[index])].cells[0].firstChild.nodeValue;

_confirmMsg_ = _confirmMsg_.concat(_deletName_ "n");

}

_confirmMsg_ = _confirmMsg_.concat("的信息吗?");

if (!confirm(_confirmMsg_)) return;

 

for (var index = _arrSelect_.length - 1; index >= 0; index--)

{

_oTbody_.deleteRow(parseInt(_arrSelect_[index]));

}

}

_arrSelect_.splice(0,_arrSelect_.length); //清空选择列表

};

//更新:(红色部分为更新的代码)

//更新

var $oUpdate = document.getElementById("btnUpdate");

$oUpdate.onclick = function()

{

var _oPreUpdateIndex_=_oTempValue_["$updateIndex"]

if (parseInt(_oPreUpdateIndex_)== -1){alert("您未编辑任何更新行!") ;return;}

if (_OTable_ && _oTbody_ )

{

if(confirm("您确定修改吗?"))

{

var _temprow_= _oTbody_.rows[parseInt(_oPreUpdateIndex_)];

var $namenode=_temprow_.cells[0].firstChild;

var $namenodevalue=document.all?$namenode.getAttribute("value"):$namenode.value;

if(!$namenodevalue||!$namenodevalue.trim()){ alert("姓名不能为空,请重新编辑!"); $namenode.focus(); return;}

for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_ )

{

var $tmpnode_=_temprow_.cells[_cellindex_].firstChild;

var $nodedata_=document.all?$tmpnode_.getAttribute("value"):$tmpnode_.value;

var _textnode_= document.createTextNode($nodedata_);

var _oldNode_=_temprow_.cells[_cellindex_].firstChild;

_temprow_.cells[_cellindex_].replaceChild(_textnode_,_oldNode_);

delete _oTempValue_["$" _cellindex_];

}

}

}

_oTempValue_["$updateIndex"] = -1

};

//查找

var $oFind = document.getElementById("btnFind");

$oFind.onclick=function()

{

if(!_OTable_ && !_oTbody_ ){alert("目前尚无数据可查!");return;}

///////////////判断之前有编辑未提交的

var _oPreUpdateIndex_=_oTempValue_["$updateIndex"];

var _oPreTempRow_=null;

if (parseInt(_oPreUpdateIndex_) != -1) //原先选定行重置

{

if (!_OTable_ || !_oTbody_) return;

_oPreTempRow_= _oTbody_.rows[parseInt(_oPreUpdateIndex_)];

var _cancelornot_=false;

for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_ )

{

var $childNode_=_oPreTempRow_.cells[_cellindex_].firstChild;

var $nodedata_=document.all?$childNode_.getAttribute("value"):$childNode_.value;

if($nodedata_!=_oTempValue_["$" _cellindex_])//与原值比较

{

_cancelornot_=confirm("你之前的内容作了修改,保存修改吗?");

break;

}

}

if(_cancelornot_)

{

//避免前次提交为空

var _firstNode_=_oPreTempRow_.cells[0].firstChild;

var $firstnodedata_=document.all?_firstNode_.getAttribute("value"):_firstNode_.value;

if(!$firstnodedata_||!$firstnodedata_.trim()){alert("姓名不能为空,请重新编辑!"); _firstNode_.focus(); return;};

for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_ )

{

var _oldNode_=_oPreTempRow_.cells[_cellindex_].firstChild;

var $nodedata_=document.all?_oldNode_.getAttribute("value"):_oldNode_.value;

var _textnode_= document.createTextNode($nodedata_);

_oPreTempRow_.cells[_cellindex_].replaceChild(_textnode_,_oldNode_);

delete _oTempValue_["$" _cellindex_];

}

}

else

{

for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_ )

{

var _oldNode_=_oPreTempRow_.cells[_cellindex_].firstChild;

var _textnode_= document.createTextNode(_oTempValue_["$" _cellindex_]);

_oPreTempRow_.cells[_cellindex_].replaceChild(_textnode_,_oldNode_);

delete _oTempValue_["$" _cellindex_];

}

}

}

//清除更新列表

for(var $obj_ in _oTempValue_)

{

delete _oTempValue_[$obj_];

}

// _oTempValue_=new Object;

_oTempValue_["$updateIndex"] = -1;

////////////////////////开始查询

var _$oSelect_= document.getElementById("selectCol");

var _Index_=_$oSelect_.selectedIndex;

var _$oSelectValue_= _$oSelect_.value;

var _$oSelectText_= _$oSelect_.options[_Index_].text;

var _$olike_=document.getElementById("Col9");

var _$rowcollection_=_oTbody_.rows;

var _$rLen=_$rowcollection_.length;

switch(parseInt(_$oSelectValue_))

{

case 0:

for(var _rIndex=0;_rIndex<_$rLen;_rIndex )

{

var _selectrow_=_$rowcollection_[_rIndex];

var $pat = new RegExp(_$olike_.value.trim(),"i");

if(!_$olike_.value.trim()){_selectrow_.style.display=document.all?"block":"table-row";}//如果查询框为空,则全部提取..模糊搜索

else {if(!$pat.test(_selectrow_.cells[0].firstChild.nodeValue.trim())){

_selectrow_.style.display="none";}}

}

break;

case 1:

for(var _rIndex=0;_rIndex<_$rLen;_rIndex )

{

var _selectrow_=_$rowcollection_[_rIndex];

var $pat = new RegExp(_$olike_.value.trim(),"i");

if(!_$olike_.value.trim()){_selectrow_.style.display=document.all?"block":"table-row";}

else

{if(!$pat.test(_selectrow_.cells[1].firstChild.nodeValue.trim()))

{_selectrow_.style.display="none";}}

}

break;

case 2:

for(var _rIndex=0;_rIndex<_$rLen;_rIndex )

{

var _selectrow_=_$rowcollection_[_rIndex];

var $pat = new RegExp(_$olike_.value.trim(),"i");

if(!_$olike_.value.trim()){_selectrow_.style.display=document.all?"block":"table-row";}

else

{if(!$pat.test(_selectrow_.cells[2].firstChild.nodeValue.trim()))

{ _selectrow_.style.display="none";}}

}

break;

//更新(红色部分为更新的)

case 3:

for(var _rIndex=0;_rIndex<_$rLen;_rIndex )

{

var _selectrow_=_$rowcollection_[_rIndex];

var $pat = new RegExp(_$olike_.value.trim(),"i");

if(!_$olike_.value.trim()){_selectrow_.style.display=document.all?"block":"table-row";}

else

{if(!$pat.test(_selectrow_.cells[3].firstChild.nodeValue.trim()))

{ _selectrow_.style.display="none";}}

}

break;

}

_arrSelect_.splice(0,_arrSelect_.length);//清除删除列表

var _checkBoxList_=document.getElementsByTagName("input"); //重置checkbox选择.

for(var _index=0,iLen=_checkBoxList_.length;_index<iLen;_index )

{

if(_checkBoxList_[_index].type=="checkbox")

{

_checkBoxList_[_index].checked=false;

}

}

};

var $oSelectAll = document.getElementById("btnSelectAll");

$oSelectAll.onclick=function()

{

if(_OTable_ && _oTbody_ )

{

var _$rowall_=_oTbody_.rows;

for(var _rIndex=0,_rLen=_$rowall_.length;_rIndex<_rLen;_rIndex )

{

var _selectrow_=_$rowall_[_rIndex];

_selectrow_.style.display=document.all?"block":"table-row";

}

}

}

}

</script>

</head>

<body>

<fieldset>

<legend>操作Table之增删查改</legend>

<fieldset>

<legend>添加</legend>

<label for="Col1">

姓名:

</label>

<input type="text" id="Col1" />

<label for="Col2">

性别:

</label>

<input type="text" id="Col2" />

<label for="Col3">

年龄:

</label>

<input type="text" id="Col3" />

<label for="Col4">

籍贯:

</label>

<input type="text" id="Col4" />

<input type="button" value="添加" id="btnAdd" />

</fieldset>

<fieldset>

<legend>查找</legend>

<label for="Col9">

查找内容:

</label>

<script type="text/javascript">

var options = ["<option value="0" selected>姓名</option>", "<option value="1">性别</option>", "<option value="2">年龄</option>", "<option value="3">籍贯</option>"];

document.write("<select name="selectCol" id="selectCol">" options.join("") "</select>");

</script>

<input type="text" id="Col9" />

<input type="button" value="查找" id="btnFind" />

</fieldset>

</fieldset>

<br />

<fieldset id="TableData">

<legend>表格数据</legend>

</fieldset>

<input type="button" value="删除" id="btnDelete" />

<input type="button" value="更新" id="btnUpdate" />

<input type="button" value="显示全部" id="btnSelectAll" />

</body>

</html>

期待本文所述对我们的javascript程序设计具备助于。

本文实例叙述了javascript实现表格增加和删除改操作的章程。分享给我们供大家参照他事他说加以考察。具体实现方式如下:...

连带作品

  • javascript达成点击商品列表checkbox实时总括
  • javascript贯彻Table排序的主意
  • Javascript动态创建表格及删除行列的章程
  • javascript制作的滑行图片菜单
  • Javascript通过overflow调节列表闭合与张开的
  • javascript完毕日期按月度加减

连带寻找: javascript 全选 反选及删除表格的措施

明天看吗

查找才干库

回来首页

  • javascript贯彻点击商品列表checkbox实时总计金额
  • javascript得以实现Table排序的秘籍
  • Javascript动态创建表格及删除行列的艺术
  • javascript制作的滑行图片菜单
  • Javascript通过overflow调控列表闭合与张开的办法
  • javascript达成日期按月度加减

连带频道: HTML/CSS  HTML5  Javascript  jQuery  AJax教程  前面一个代码  正则表明式  Flex教程  WEB前端教程  

  .line{ display:none; z-index:1; left:0; top:0; position:absolute;}#line1{ display:block;}.container{ position:relative; width:564px; height:294px; overflow:hidden;}.border{ border:5px solid #000;}.corner{ position:absolute; width:282px; height:147px; background:#ccc; overflow:hidden;}.leftTop,.inLeftTop{ position:absolute; left:0; top:0; right:auto; bottom:auto;}.rightTop,.inRightTop{ position:absolute; right:0; top:0; left:auto; bottom:auto;}.rightBottom,.inRightBottom{ position:absolute; right:0; bottom:0; top:auto; left:auto;}.leftBottom,.inLeftBottom{ position:absolute; left:0; bottom:0; top:auto; right:auto;}function crossLine(container,option,callback){var lineX=$("&lt;div style='width:2000px;height:4px;overflow:hidden;position:absolute;background:#FACC41;left:0;top:0;z-index:1'&gt;&lt;/div&gt;");var lineY=$("&lt;div style='width:4px;height:2000px;overflow:hidden;position:absolute;background:#FACC41;left:0;top:0;z-index:1'&gt;&lt;/div&gt;");var _option={ "display":"none", "targetPosX":container.width()/2-2, "targetPosY":container.height()/2-2, "time":500, "freq":10};$.extend;option=_option;var targetPosX=option.targetPosX;var targetPosY=option.targetPosY;var time=option.time;var freq=option.freq;times=time/freq;container.append;//&#24320;&#22987;&#36816;&#21160;var lxt=lineX.position().top;var lyl=lineY.position().left;xPerTime=targetPosX/times;yPerTime=targetPosY/times;var count=0;var si=setInterval{ count  ; if{ clearInterval; if=="function"){ callback(); } if(option.display=="none"){ lineX.remove; } } if(lxt yPerTime&lt;=targetPosY){ lxt  = yPerTime; lineX.css; }else{ lxt=targetPosY; lineX.css; } if(lyl xPerTime&lt;=targetPosX){ lyl  = xPerTime; lineY.css; }else{ lyl=targetPosX; lineY.css; }},freq);}function picSplit(line1,line2,container,option){//beginline1.css;/*var targetX=282;var targetY=147;*/var _option={ "targetX":container.width()/2, "targetY":container.height()/2, "time":500, "freq":10};$.extend;option=_option;var targetX=option.targetX;var targetY=option.targetY;var containerWidth=container.width();var containerHeight=container.height();/**&#22797;&#21046;&#22235;&#20010;&#65292;&#25918;&#20837;&#22235;&#20010;&#23481;&#22120;&#65292;&#32622;&#20110;&#22235;&#35282;&#65292;&#28982;&#21518;&#31227;&#21160;*///div0-4 &#23481;&#22120;&#65292;&#25918;&#32622;&#20110;&#22235;&#35282;var div0=$.css({"position":"absolute","left":0,"top":0,"right":"auto","bottom":"auto","width":targetX,"height":targetY,"z-index":"2","overflow":"hidden"}).appendTo;var div1=$.css({"position":"absolute","left":targetX,"top":0,"right":"auto","bottom":"auto","width":containerWidth-targetX,"height":targetY,"z-index":"2","overflow":"hidden"}).appendTo;var div2=$.css({"position":"absolute","left":targetX,"top":targetY,"right":"auto","bottom":"auto","width":targetX,"height":containerHeight-targetY,"z-index":"2","overflow":"hidden"}).appendTo;var div3=$.css({"position":"absolute","left":0,"top":targetY,"right":"auto","bottom":"auto","width":targetX,"height":containerHeight-targetY,"z-index":"2","overflow":"hidden"}).appendTo;//tempL0-4&#22797;&#21046;&#20986;&#26469;&#30340;&#23618;var tempL0=line1.clone().css({"position":"absolute","left":0,"top":0,"right":"auto","bottom":"auto","z-index":"2"}).appendTo;var tempL1=line1.clone().css({"position":"absolute","left":-targetX,"top":0,"right":"auto","bottom":"auto","z-index":"2"}).appendTo;var tempL2=line1.clone().css({"position":"absolute","left":-targetX,"top":-targetY,"right":"auto","bottom":"auto","z-index":"2"}).appendTo;var tempL3=line1.clone().css({"position":"absolute","left":0,"top":-targetY,"right":"auto","bottom":"auto","z-index":"2"}).appendTo;line1.css;line2.css;//&#24320;&#22987;&#36816;&#21160;var time=option.time;var freq=option.freq;var times=time/freq;var count=0;var xLeftPerTime=3;var xRightPerTime=3;var yTopPerTime=3;var yBottomPerTime=3;var l0=div0.position().left;var t0=div0.position().top;var l1=div1.position().left;var t1=div1.position().top;var l2=div2.position().left;var t2=div2.position().top;var l3=div3.position().left;var t3=div3.position().top;var si=setInterval{ count  ; if{ clearInterval; div1.remove; div3.remove(); } l0=l0-xLeftPerTime; t0=t0-yTopPerTime; l1=l1 xRightPerTime; t1=t1-yTopPerTime; l2=l2 xRightPerTime; t2=t2 yBottomPerTime; l3=l3-xLeftPerTime; t3=t3 yBottomPerTime; div0.css("left", "px"); div0.css("top",; div1.css("left", "px"); div1.css("top",; div2.css("left", "px"); div2.css("top", "px"); div3.css("left", "px"); div3.css("top", "px");},freq);}var line1=$;var line2=$;line1.css;var container=$;function go(){ var option={"display":"none"}; crossLine(container,option,gopicSplit);}var container=$;function gopicSplit(){ picSplit(line1,line2,container);}

相关小说

  • javascript落到实处可全选、反选及删除表格的方
  • javascript兑现点击商品列表checkbox实时计算
  • javascript落到实处Table排序的不二秘籍
  • Javascript动态创建表格及删除行列的方法
  • javascript制作的滑动图片菜单
  • Javascript通过overflow调控列表闭合与拓宽的

有关搜索: javascript 表格

后日看甚

找出手艺库

回来首页

  • javascript达成可全选、反选及删除表格的主意
  • javascript贯彻点击商品列表checkbox实时总结金额
  • javascript兑现Table排序的不二诀窍
  • Javascript动态创立表格及删除行列的方法
  • javascript制作的滑动图片菜单
  • Javascript通过overflow调控列表闭合与开展的法门

连锁频道: HTML/CSS  HTML5  Javascript  jQuery  AJax教程  前面二个代码  正则表明式  Flex教程  WEB前端教程  

javascript贯彻可全选、反选及删除表格的主意

 本文实例呈报了javascript完结可全选、反选及删除表格的艺术。分享给我们供我们参谋。具体得以实现情势如下:

?

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>可以全选反选删除的表格</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">

//定义嵌套数组

var datas = [["张三", 30, "南昌"], ["李四", 25, "北京"], ["王五", 20, "郑州"],

["赵六", 19, "武汉"], ["李莫", 18, "深圳"], ["罗成", 33, "重庆"],

["王平", 31, "天津"], ["刘萍", 22, "上海"], ["杨丽", 17, "石家庄"],

["郭丽", 30, "广州"]];

//动态创建表格

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);//控制数据行添加到倒数第二列

addCol = addRow.insertCell(-1);

addCol.innerHTML = "<input type='checkbox' name='item' />";

addCol.align = "center"; //控制列居中

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='删除' onclick='btnDel(this)' />";

addCol.align = "center"; //控制列居中

rowsCount ;

}

TableColor();

}

//设置网格间隔色和高亮显示

var oldClassName; //记住行的背景色

function TableColor() {

var tblMain = document.getElementById("tblMain");

var rowNodes = tblMain.rows;

for (var i = 1; i < rowNodes.length-1; i ) { //除去首行和末行

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;

}

}

}

//复选框全选函数

function checkAll() {

var currentCheckNode = event.srcElement;

var checkAllNodes = document.getElementsByName("all");

//把没有点击的全选复选框去除复选

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;

}

}

//按钮选择函数

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;

}

}

}

//每行的删除按钮函数

function btnDel(btn) {

var tblMain = document.getElementById("tblMain");

var delRowNode = btn.parentNode.parentNode;

var sMsg = "您是否要删除 姓名为:【" delRowNode.cells[1].innerText "】,年龄为:【"

delRowNode.cells[2].innerText "】,城市为:【"

delRowNode.cells[3].innerText "】 的数据?";

if (window.confirm(sMsg)) {

tblMain.tBodies[0].removeChild(delRowNode);

TableColor();

}

}

//删除所选项按钮函数

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("是否要删除选择的数据?"))

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()" />全选</th>

<th>姓名</th>

<th>年龄</th>

<th>城市</th>

<th>操作</th>

</tr>

<tr>

<th><input type="checkbox" name="all" onclick="checkAll()" />全选</th>

<th colspan="4">

<input type="button" value="全选" onclick="btnCheckboxSel(1)" />

<input type="button" value="全清" onclick="btnCheckboxSel(0)" />

<input type="button" value="反选" onclick="btnCheckboxSel(2)" />

<input type="button" value="删除所选项" onclick="btnDelSelectRow()" />

</th>

</tr>

</tbody>

</table>

</body>

</html>

愿意本文所述对大家的javascript程序设计有着帮忙。

本文实例叙述了javascript达成可全选、反选及删除表格的措施。分享给我们供大家参谋。具体实...

正文实例汇报了javascript达成的图样切割多块效果。共享给我们供我们参照他事他说加以调查。具体落到实处格局如下:

本文由时时app平台注册网站发布于彩世界网址,转载请注明出处:javascript贯彻可全选、反选及删除表格的不二等秘

关键词: