1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head id="Head1" runat="server">
  6. <title></title>
  7. <style type="text/css">
  8. .select
  9. {
  10. background-color: gray;
  11. }
  12. .left
  13. {
  14. text-align: left;
  15. }
  16. .center
  17. {
  18. text-align: center;
  19. }
  20. .right
  21. {
  22. text-align: right;
  23. }
  24. table
  25. {
  26. border-collapse: collapse;
  27. border: none;
  28. }
  29. td
  30. {
  31. border: solid # 1px;
  32. border-color: Black;
  33. empty-cells: show;
  34. }
  35. </style>
  36. <script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
  37. <script type="text/javascript">
  38. $(function () {
  39. var createTabl = function () {
  40. $("table tbody tr").remove();
  41. var j = ;
  42. while (j < ) {
  43. var i = ;
  44. var tr = $("<tr></tr>");
  45. tr.attr("y", j);
  46. while (i < ) {
  47. var td = $("<td>" + j + "." + i + "</td>");
  48. td.attr({ x: i, y: j });
  49. td.click(function (event) { clickTd(event); });
  50. tr.append(td);
  51. i++;
  52. }
  53. $("table").append(tr);
  54. j++;
  55. };
  56. };
  57. createTabl();
  58.  
  59. var clickTd = function (event) {
  60. var obj = event.srcElement ? event.srcElement : event.target;
  61. $(obj).toggleClass("select");
  62. if (event.ctrlKey == ) {
  63. var rangetd = rangeTD();
  64. $("table").find("td").each(function () {
  65. $(this).removeClass("select");
  66. var x = parseInt($(this).attr("x"));
  67. var y = parseInt($(this).attr("y"));
  68. if (x >= rangetd.x && x <= (rangetd.x + rangetd.xCount - ) && y >= rangetd.y && y <= (rangetd.y + rangetd.yCount - )) {
  69. $(this).addClass("select");
  70. }
  71. });
  72. }
  73. };
  74.  
  75. $("#create").click(function () { createTabl() });
  76.  
  77. var getMax = function (values) {
  78. var temp = ;
  79. for (var i = ; i < values.length; i++) {
  80. if (i == ) {
  81. temp = values[i];
  82. } else {
  83. if (values[i] > temp) {
  84. temp = values[i];
  85. }
  86. }
  87. }
  88. return temp;
  89. }
  90. var getMin = function (values) {
  91. var temp = ;
  92. for (var i = ; i < values.length; i++) {
  93. if (i == ) {
  94. temp = values[i];
  95. } else {
  96. if (values[i] < temp) {
  97. temp = values[i];
  98. }
  99. }
  100. }
  101. return temp;
  102. }
  103.  
  104. $("#split").click(function () {
  105. //补齐合并的列
  106. $(".select[colspan]").each(function () {
  107. var x = parseInt($(this).attr("x")) + ;
  108. var y = parseInt($(this).attr("y"));
  109. var colspan = parseInt($(this).attr("colspan"));
  110. var td = $(this);
  111. while (colspan > ) {
  112. var newTd = getTd(x, y);
  113. td.after(newTd);
  114. td = newTd;
  115. colspan--;
  116. x++;
  117. }
  118. });
  119.  
  120. //补齐合并的行
  121. $(".select[rowspan]").each(function () {
  122. var colspan = ;
  123. if ($(this).attr("colspan") != undefined) {
  124. colspan = parseInt($(this).attr("colspan"));
  125. }
  126. var y = parseInt($(this).attr("y")) + ;
  127. var rowspan = parseInt($(this).attr("rowspan"));
  128. while (rowspan > ) {
  129. var x = parseInt($(this).attr("x"));
  130. var tr = $("table tr td[y='" + y + "']:first").parent();
  131.  
  132. var td;
  133. tr.find("td").each(function (i, o) {
  134. var nextX = parseInt($(this).attr("x"));
  135. if (nextX < x) {
  136. td = $(this);
  137. }
  138. });
  139.  
  140. var temp_colspan = colspan;
  141.  
  142. while (temp_colspan > ) {
  143. var newTd = getTd(x, y);
  144. td.after(newTd);
  145. td = newTd;
  146. x++;
  147. temp_colspan--;
  148. }
  149. rowspan--;
  150. y++;
  151. }
  152. });
  153.  
  154. $(".select[rowspan]").removeAttr("rowspan");
  155. $(".select[colspan]").removeAttr("colspan");
  156. $(".select").removeClass("select");
  157. });
  158.  
  159. var getTd = function (x, y) {
  160. var td = $("<td>" + y + "." + x + "</td>");
  161. td.attr({ x: x, y: y });
  162. td.click(function (event) { clickTd(event); });
  163. return td;
  164. }
  165.  
  166. $("#merge").click(function () {
  167. if (canMeger()) {
  168. var range_td = rangeTD();
  169. if (range_td.xCount > ) {
  170. $(".select:first").attr("colspan", range_td.xCount);
  171. }
  172. if (range_td.yCount > ) {
  173. $(".select:first").attr("rowspan", range_td.yCount);
  174. }
  175. $(".select:gt(0)").remove();
  176. $(".select").removeClass("select");
  177. } else {
  178. alert("不能合并");
  179. }
  180. });
  181.  
  182. var rangeTD = function () {
  183. var xValues = [];
  184. var yValues = [];
  185. $(".select").each(function () {
  186. xValues.push(parseInt($(this).attr("x")));
  187. yValues.push(parseInt($(this).attr("y")));
  188. });
  189. var maxX = getMax(xValues);
  190. var maxY = getMax(yValues);
  191. var minX = getMin(xValues);
  192. var minY = getMin(yValues);
  193. return { x: minX, y: minY, xCount: maxX - minX + , yCount: maxY - minY + };
  194. };
  195.  
  196. var canMeger = function () {
  197. var range_td = rangeTD();
  198. var selectCount = ;
  199. $(".select").each(function () {
  200. var rowspan = ;
  201. var colspan = ;
  202. if ($(this).attr("rowspan") != undefined) {
  203. rowspan = parseInt($(this).attr("rowspan"));
  204. }
  205. if ($(this).attr("colspan") != undefined) {
  206. colspan = parseInt($(this).attr("colspan"));
  207. }
  208. selectCount += (rowspan * colspan);
  209. });
  210. return selectCount == (range_td.xCount * range_td.yCount);
  211. }
  212.  
  213. $(".align").click(function () {
  214. var textAlign = $(this).attr("gh-align");
  215. $(".select").css("text-align", textAlign);
  216. $(".select").removeClass("select");
  217. });
  218.  
  219. $("#insertRow").click(function () {
  220. var tr = $(".select").parent();
  221. tr.find(".select").removeClass("select");
  222. var trCopy = tr.clone(true);
  223. trCopy.find("td[rowspan]").removeAttr("rowspan");
  224. trCopy.find("td[colspan]").each(function () {
  225. var x = parseInt($(this).attr("x"));
  226. var y = parseInt($(this).attr("y"));
  227. var colspan = parseInt($(this).attr("colspan"));
  228. var td = $(this);
  229. while (colspan > ) {
  230. td.after(getTd(x + , y));
  231. td = td.next();
  232. colspan--;
  233. }
  234. });
  235. var trIndex = parseInt(tr.find("td:first").attr("y"));
  236. tr.prevAll().find("td[rowspan]").each(function () {
  237. var rowspan = parseInt($(this).attr("rowspan"));
  238. var tdY = parseInt($(this).attr("y")) + rowspan - ;
  239. if (tdY >= trIndex) {
  240. $(this).attr("rowspan", rowspan + );
  241. }
  242. });
  243.  
  244. trCopy.find("td[colspan]").removeAttr("colspan");
  245. trCopy.find("td").empty();
  246. trCopy.find("td").append("&nbsp;");
  247. tr.before(trCopy);
  248.  
  249. trCopy.nextAll().find("td").each(function () {
  250. var y = parseInt($(this).attr("y")) + ;
  251. $(this).attr("y", y);
  252. });
  253. });
  254.  
  255. $("#delRow").click(function () {
  256. var tr = $(".select").parent();
  257. //删除合并行的第一行
  258. tr.find("td[rowspan]").each(function () {
  259. var tdCopy = $(this).clone(true);
  260. var rowspan = parseInt($(this).attr("rowspan"));
  261. if ((rowspan - ) == ) {
  262. tdCopy.removeAttr("rowspan");
  263. } else {
  264. tdCopy.attr("rowspan", rowspan - );
  265. }
  266.  
  267. tdCopy.attr("y", parseInt($(this).attr("y")) + );
  268. var delX = parseInt($(this).attr("x"));
  269. var td = null;
  270. tr.next().find("td").each(function () {
  271. var x = parseInt($(this).attr("x"));
  272. if (x < delX) {
  273. td = $(this);
  274. }
  275. });
  276. if (td == null) {
  277. tr.prepend(tdCopy);
  278. } else {
  279. td.after(tdCopy);
  280. }
  281.  
  282. });
  283.  
  284. var trIndex = parseInt(tr.find("td:first").attr("y"));
  285. tr.prevAll().find("td[rowspan]").each(function () {
  286. var rowspan = parseInt($(this).attr("rowspan"));
  287. var tdY = parseInt($(this).attr("y")) + rowspan - ;
  288. if (tdY >= trIndex) {
  289. if ((rowspan - ) == ) {
  290. $(this).removeAttr("rowspan");
  291. } else {
  292. $(this).attr("rowspan", rowspan - );
  293. }
  294. }
  295. });
  296.  
  297. tr.nextAll().find("td").each(function () {
  298. var y = parseInt($(this).attr("y")) - ;
  299. $(this).attr("y", y);
  300. });
  301. tr.remove();
  302. });
  303.  
  304. $("#insertCol").click(function () {
  305. var x = parseInt($(".select").attr("x"));
  306. $("table tbody tr").each(function () {
  307. var td = $(this).find("td[x='" + x + "']");
  308. if (td.size() > ) {
  309. var newTd = getTd(x, td.attr("y"));
  310. td.before(newTd);
  311. td = newTd;
  312. } else {
  313. $(this).find("td").each(function () {
  314. var tdX = parseInt($(this).attr("x"));
  315. if (tdX < x) {
  316. td = $(this);
  317. }
  318. });
  319. td.attr("colspan", parseInt(td.attr("colspan")) + );
  320. }
  321. td.nextAll().each(function () {
  322. $(this).attr("x", parseInt($(this).attr("x")) + );
  323. });
  324. });
  325. });
  326. $("#delCol").click(function () {
  327. var x = parseInt($(".select").attr("x"));
  328. $("table tbody tr").each(function () {
  329. var td = $(this).find("td[x='" + x + "']");
  330. if (td.size() > ) {
  331. td.nextAll().each(function () {
  332. $(this).attr("x", parseInt($(this).attr("x")) - );
  333. });
  334. if (td.attr("colspan") == undefined) {
  335. td.remove();
  336. } else {
  337. var colspan = parseInt(td.attr("colspan")) - ;
  338. if (colspan == ) {
  339. td.removeAttr("colspan");
  340. } else {
  341. td.attr("colspan", colspan);
  342. }
  343. }
  344. } else {
  345. $(this).find("td").each(function () {
  346. var tdX = parseInt($(this).attr("x"));
  347. if (tdX < x) {
  348. td = $(this);
  349. }
  350. });
  351. if (td.attr("colspan") != undefined) {
  352. var colspan = parseInt(td.attr("colspan")) - ;
  353. if (colspan == ) {
  354. td.removeAttr("colspan");
  355. } else {
  356. td.attr("colspan", colspan);
  357. }
  358. }
  359. td.nextAll().each(function () {
  360. $(this).attr("x", parseInt($(this).attr("x")) - );
  361. });
  362. }
  363. });
  364. });
  365.  
  366. $("table").on("mousedown", function () {
  367. if (window.event.button == ) {
  368. $(this).find(".select").removeClass("select");
  369. }
  370. });

$("table").on("contextmenu", function () {
return false;
});

  1. $("#test").on("click", function () {
  2. $("table td").each(function () {
  3. $(this).empty();
  4. $(this).append($(this).attr("y") + "." + $(this).attr("x"));
  5. });
  6. });
  7. });
  8. </script>
  9. </head>
  10. <body>
  11. <form id="form1" runat="server">
  12. <div>
  13. <div class="tool">
  14. <input type="button" value="新建" id="create" />
  15. <input type="button" value="合并" id="merge" />
  16. <input type="button" value="拆分" id="split" />
  17. <input type="button" value="插入行" id="insertRow" />
  18. <input type="button" value="删除行" id="delRow" />
  19. <input type="button" value="插入列" id="insertCol" />
  20. <input type="button" value="删除列" id="delCol" />
  21. <input type="button" value="左对齐" class="align" gh-align="left" />
  22. <input type="button" value="居中" class="align" gh-align="center" />
  23. <input type="button" value="右对齐" class="align" gh-align="right" />
  24. <input type="button" value="验证" id="test" />
  25. </div>
  26. <div class="body">
  27. <table border="" style="width: 100%;">
  28. </table>
  29. </div>
  30. </div>
  31. </form>
  32. </body>
  33. </html>

程序员的基础教程:菜鸟程序员

js 操作table的更多相关文章

  1. 第十三篇 JS 操作table表格

    JS 操作table表格 这节课难度可能高一点,因为没有提前解释if判断.for循环.这节课是直接把这两样用上了,老师先简单介绍一下: if,判断语句,判断就很简单了嘛,假如说1=1(1等于1),当然 ...

  2. js操作table倒叙显示序号的问题

    今天遇到一奇葩问题,就是在js添加table时,序号是倒叙显示的,而且数据库查出来时正序的,为什么显示是倒叙的呢? 我百度一番,终于有了结果: var newRow=table.insertRow(- ...

  3. js 操作table: insertRow(),deleteRow(),insertCell(),deleteCell()方法

    表格有几行: var trCnt = table.rows.length;  (table为Id ) 每行有几列:for (var i=0; i<trCnt; i++)              ...

  4. js操作table表格导出数据到excel方法

    js导出excel资料很少,网上也找了很多,基本都不能用,要么只能是IE用,还必须要权限,这是非常不好的.后来到github上找到table2excel.js,虽然可以用,但仍然对IE支持不够,也算不 ...

  5. js操作table

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. 原生JS操作 table object HTMLTableSectionElement 对象,获取行数

    <tbody id="infoTab"> <tr class="fomat"> <td class="blank&quo ...

  7. js操作table中tr的顺序,实现上移下移一行的效果

    总体思路是在table外部加个div,修改div的innerHtml实现改变tr顺序的效果 具体思路是 获取当前要移动tr行的rowIndex,在table中删除掉,然后循环table的rows,到了 ...

  8. js操作表格

    js 操作table: insertRow(),deleteRow(),insertCell(),deleteCell()方法 表格有几行: var trCnt = table.rows.length ...

  9. js实现动态操作table

     本章案例为通过js,动态操作table,实现在单页面进行增删改查的操作. 简要案例如下: <%@ page language="java" contentType=&quo ...

随机推荐

  1. rebar自定义template

    在开发过程中rebar自带模板建立项目,或多或少不能满足自己的开发需求.本人又是那种懒人,所以就要想办法偷懒.查看了priv模板 打造适合自己的项目模板.下面我简单的介绍整个模板的打造过程. 准备过程 ...

  2. LOJ10042 收集雪花

    题意 不同的雪花往往有不同的形状.在北方的同学想将雪花收集起来,作为礼物送给在南方的同学们.一共有 n 个时刻,给出每个时刻下落雪花的形状,用不同的整数表示不同的形状.在收集的过程中,同学们不希望有重 ...

  3. maven编译问题:maven编译成功,eclipse文件未编译

    我们先来看一个正常的编译流程: 1.从svn上检出一个项目: 2.看该工程是否为maven项目,不是则先转为maven项目:右键单击项目,选择configure->Convert to Mave ...

  4. erlang配置三方库

    暴力的: 直接下载解压以后放到erlang的lib目录,比如/usr/local/Cellar/erlang/17.3/lib/erlang/lib 和谐的: 在用户名下建立.erlang文件 在里面 ...

  5. 【转】JMeter入门

    一.JMeter概述 JMeter就是一个测试工具,相比于LoadRunner等测试工具,此工具免费,且比较好用,但是前提当然是安装Java环境: JMeter可以做 (1)压力测试及性能测试: (2 ...

  6. java从键盘输入一组数据,输出其最大值,平均值,最小值没法输出

    总结::需要耐心,加思考.做事不思考,那就是白做徒劳!!!!! package com.aini; import java.util.Scanner; //操...为什么数组的大小比较我硬是搞不懂,比 ...

  7. PHP base64多图片上传

    // 多图片上传,base64 public function upload_multi() { $pic = $_POST['pic']; if (!$pic) { $this->json-& ...

  8. git的分布式和集中式

    当然,Git的优势不单是不必联网这么简单,后面我们还会看到Git极其强大的分支管理,把SVN等远远抛在了后面.

  9. 第三章 深入分析Java Web中的中文编码问题

    3.1 几种常见的编码格式 3.1.1 为什么要编码 一个字节 byte只能表示0~255个符号,要表示更多的字符,需要编码. 3.1.2 如何翻译 ASCII码:有128个,用一个字节的低7位表示. ...

  10. 我是怎么用python模仿勒索软件加密文件的(病毒)

    前言: 今天下午上学,用python写个勒索脚本然后打包成exe是个不错的选择 我们来搞事情吧.看那学校我就不想上学. 0x01:要用到的模块,各位请自行准备 import win32api,win3 ...