> 웹 프론트엔드 > JS 튜토리얼 > HTML 테이블 행 및 열 잠금을 구현하기 위해 js를 사용하는 간단한 예

HTML 테이블 행 및 열 잠금을 구현하기 위해 js를 사용하는 간단한 예

高洛峰
풀어 주다: 2016-12-29 16:25:33
원래의
1493명이 탐색했습니다.

다음 편집기는 HTML 테이블 행 및 열 잠금의 JS 구현에 대한 간단한 예를 제공합니다. 편집자님이 꽤 좋다고 생각하셔서 지금 공유하고 모두에게 참고용으로 드리고자 합니다. 에디터를 따라가서 살펴봅시다.

js实现html table 行,列锁定的简单实例

관심 있는 친구들은 직접 복사해서 실행해 보면 효과를 볼 수 있습니다.

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

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

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

  <title>new document</title>

  <script type="text/javascript"

      src="http://code.jquery.com/jquery-1.6.1.min.js"></script>

    

  <script type="text/javascript">

    function FixTable(TableID, FixColumnNumber, width, height) {

      /// <summary>

      ///   锁定表头和列

      ///   <para> sorex.cnblogs.com </para>

      /// </summary>

      /// <param name="TableID" type="String">

      ///   要锁定的Table的ID

      /// </param>

      /// <param name="FixColumnNumber" type="Number">

      ///   要锁定列的个数

      /// </param>

      /// <param name="width" type="Number">

      ///   显示的宽度

      /// </param>

      /// <param name="height" type="Number">

      ///   显示的高度

      /// </param>

      if ($("#" + TableID + "_tableLayout").length != 0) {

        $("#" + TableID + "_tableLayout").before($("#" + TableID));

        $("#" + TableID + "_tableLayout").empty();

      }

      else {

        $("#" + TableID).after("<div id=&#39;" + TableID + "_tableLayout&#39; style=&#39;overflow:hidden;height:" + height + "px; width:" + width + "px;&#39;></div>");

      }

      $(&#39;<div id="&#39; + TableID + &#39;_tableFix"></div>&#39;

      + &#39;<div id="&#39; + TableID + &#39;_tableHead"></div>&#39;

      + &#39;<div id="&#39; + TableID + &#39;_tableColumn"></div>&#39;

      + &#39;<div id="&#39; + TableID + &#39;_tableData"></div>&#39;).appendTo("#" + TableID + "_tableLayout");

      var oldtable = $("#" + TableID);

      var tableFixClone = oldtable.clone(true);

      tableFixClone.attr("id", TableID + "_tableFixClone");

      $("#" + TableID + "_tableFix").append(tableFixClone);

      var tableHeadClone = oldtable.clone(true);

      tableHeadClone.attr("id", TableID + "_tableHeadClone");

      $("#" + TableID + "_tableHead").append(tableHeadClone);

      var tableColumnClone = oldtable.clone(true);

      tableColumnClone.attr("id", TableID + "_tableColumnClone");

      $("#" + TableID + "_tableColumn").append(tableColumnClone);

      $("#" + TableID + "_tableData").append(oldtable);

      $("#" + TableID + "_tableLayout table").each(function () {

        $(this).css("margin", "0");

      });

      var HeadHeight = $("#" + TableID + "_tableHead thead").height();

      HeadHeight += 2;

      $("#" + TableID + "_tableHead").css("height", HeadHeight);

      $("#" + TableID + "_tableFix").css("height", HeadHeight);

      var ColumnsWidth = 0;

      var ColumnsNumber = 0;

      $("#" + TableID + "_tableColumn tr:last td:lt(" + FixColumnNumber + ")").each(function () {

        ColumnsWidth += $(this).outerWidth(true);

        ColumnsNumber++;

      });

      ColumnsWidth += 2;

      if ($.browser.msie) {

        switch ($.browser.version) {

          case "7.0":

            if (ColumnsNumber >= 3) ColumnsWidth--;

            break;

          case "8.0":

            if (ColumnsNumber >= 2) ColumnsWidth--;

            break;

        }

      }

      $("#" + TableID + "_tableColumn").css("width", ColumnsWidth);

      $("#" + TableID + "_tableFix").css("width", ColumnsWidth);

      $("#" + TableID + "_tableData").scroll(function () {

        $("#" + TableID + "_tableHead").scrollLeft($("#" + TableID + "_tableData").scrollLeft());

        $("#" + TableID + "_tableColumn").scrollTop($("#" + TableID + "_tableData").scrollTop());

      });

      $("#" + TableID + "_tableFix").css({ "overflow": "hidden", "position": "relative", "z-index": "50", "background-color": "Silver" });

      $("#" + TableID + "_tableHead").css({ "overflow": "hidden", "width": width - 17, "position": "relative", "z-index": "45", "background-color": "Silver" });

      $("#" + TableID + "_tableColumn").css({ "overflow": "hidden", "height": height - 17, "position": "relative", "z-index": "40", "background-color": "Silver" });

      $("#" + TableID + "_tableData").css({ "overflow": "scroll", "width": width, "height": height, "position": "relative", "z-index": "35" });

      if ($("#" + TableID + "_tableHead").width() > $("#" + TableID + "_tableFix table").width()) {

        $("#" + TableID + "_tableHead").css("width", $("#" + TableID + "_tableFix table").width());

        $("#" + TableID + "_tableData").css("width", $("#" + TableID + "_tableFix table").width() + 17);

      }

      if ($("#" + TableID + "_tableColumn").height() > $("#" + TableID + "_tableColumn table").height()) {

        $("#" + TableID + "_tableColumn").css("height", $("#" + TableID + "_tableColumn table").height());

        $("#" + TableID + "_tableData").css("height", $("#" + TableID + "_tableColumn table").height() + 17);

      }

      $("#" + TableID + "_tableFix").offset($("#" + TableID + "_tableLayout").offset());

      $("#" + TableID + "_tableHead").offset($("#" + TableID + "_tableLayout").offset());

      $("#" + TableID + "_tableColumn").offset($("#" + TableID + "_tableLayout").offset());

      $("#" + TableID + "_tableData").offset($("#" + TableID + "_tableLayout").offset());

    }

$(document).ready(function () {

      FixTable("MyTable", 1, 600, 400);

    });

  </script>

</head>

<body>

  <table style="border-bottom-color: black; border-top-color: black; width: 1000px; color: #000000; border-right-color: black; font-size: medium; border-left-color: black"

      id="MyTable" border="1" cellspacing="0" cellpadding="0">

    <thead>

      <tr>

        <th style="text-align: center; width: 80px" rowspan="3">姓名</th>

        <th style="text-align: center; width: 80px" rowspan="3">班级</th>

        <th style="text-align: center" colspan="10">成绩</th>

      </tr>

      <tr>

        <th style="text-align: center" colspan="3">主科</th>

        <th style="text-align: center" colspan="3">文科</th>

        <th style="text-align: center" colspan="3">理科</th>

        <th style="text-align: center; width: 80px" rowspan="2">总分</th>

      </tr>

      <tr>

        <th style="text-align: center; width: 80px">语文</th>

        <th style="text-align: center; width: 80px">数学</th>

        <th style="text-align: center; width: 80px">英语</th>

        <th style="text-align: center; width: 80px">政治</th>

        <th style="text-align: center; width: 80px">历史</th>

        <th style="text-align: center; width: 80px">地理</th>

        <th style="text-align: center; width: 80px">物理</th>

        <th style="text-align: center; width: 80px">化学</th>

        <th style="text-align: center; width: 80px">生物</th>

      </tr>

      <!--

         <tr>

         <th style="width: 80px; text-align: center;" _mce_style="width: 80px; text-align: center;">

                   姓名

                 </th>

                 <th style="width: 80px; text-align: center;" _mce_style="width: 80px; text-align: center;">

                   班级

                 </th>

                 <th style="width: 80px; text-align: center;" _mce_style="width: 80px; text-align: center;">

                   语文

                 </th>

                 <th style="width: 80px; text-align: center;" _mce_style="width: 80px; text-align: center;">

                   数学

                 </th>

                 <th style="width: 80px; text-align: center;" _mce_style="width: 80px; text-align: center;">

                   英语

                 </th>

                 <th style="width: 80px; text-align: center;" _mce_style="width: 80px; text-align: center;">

                   政治

                 </th>

                 <th style="width: 80px; text-align: center;" _mce_style="width: 80px; text-align: center;">

                   历史

                 </th>

                 <th style="width: 80px; text-align: center;" _mce_style="width: 80px; text-align: center;">

                   地理

                 </th>

                 <th style="width: 80px; text-align: center;" _mce_style="width: 80px; text-align: center;">

                   物理

                 </th>

                 <th style="width: 80px; text-align: center;" _mce_style="width: 80px; text-align: center;">

                   化学

                 </th>

                 <th style="width: 80px; text-align: center;" _mce_style="width: 80px; text-align: center;">

                   生物

                 </th>

                 <th style="width: 80px; text-align: center;" _mce_style="width: 80px; text-align: center;">

                   总分

                 </th>

  

         </tr>

       -->

    </thead>

    <tbody>

      <!-- 数据行 -->

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

      <tr>

        <td>学生32</td>

        <td>班级1</td>

        <td>29</td>

        <td>25</td>

        <td>146</td>

        <td>28</td>

        <td>79</td>

        <td>73</td>

        <td>47</td>

        <td>8</td>

        <td>91</td>

        <td>526</td>

      </tr>

    </tbody>

  </table>

</body>

</html>

로그인 후 복사

위는 편집자가 가져온 HTML 테이블 행 및 열 잠금의 JS 구현에 대한 간단한 예입니다. 모두가 PHP 중국어 웹 사이트를 지원하기를 바랍니다~

추가 js 관련 html 테이블 행 및 열 잠금 구현에 대한 간단한 예에 대한 기사를 보려면 PHP 중국어 웹사이트에 주목하세요!


관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿