拖拉表格的JS

JS:

  1. /*
  2. @parem      object      the tbody's object
  3. @parem      object      tr's object (must be null)
  4. @parem      string      the className of onmousedown
  5. @parem      string      the className of onmouseout
  6. */
  7. function order(tt,old,classover,classout) {
  8.     var sf = arguments.callee; //get the function self
  9.     var trs = tt.getElementsByTagName('tr');
  10.     for(var i=0;i<trs.length;i++) {
  11.         trs[i].onmousedown = function () {
  12.             if(this.style.cursor == 'move') {
  13.                 return false;
  14.             }
  15.             classout = this.className;
  16.             this.className = classover;
  17.             this.style.cursor = 'move';
  18.             old = this;
  19.         }
  20.         trs[i].onmouseover = function () {
  21.             if(this.style.cursor == 'move' || !old) {
  22.                 return false;
  23.             }
  24.             var tmp_old = old.cloneNode(true);
  25.             var tmp_now = this.cloneNode(true);
  26.             var p = this.parentNode;
  27.             p.replaceChild(tmp_now,old);
  28.             p.replaceChild(tmp_old,this);
  29.             sf(tt,tmp_old,classover,classout);
  30.         }
  31.         trs[i].onmouseout = function () {
  32.             //this.className = classout;
  33.         }
  34.         trs[i].onmouseup = function () {
  35.             this.className = classout;
  36.             this.style.cursor = '';
  37.             old = null;
  38.         }
  39.     }
  40. }

 

示例:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>无标题文档</title>
  6. </head>
  7. <script src="js/ajax.js"></script>
  8. <script src="js/global.js"></script>
  9. <style type="text/css">
  10. .table {
  11.     background-color:red;
  12. }
  13. .table td {
  14.     background-color:#eeeeee;
  15. }
  16. .now td{
  17.     background-color:red;
  18. }
  19. </style>
  20. <script type="text/javascript">
  21. <!--
  22. window.onload = function () {
  23.     order(document.getElementById('tt'),null,"now");
  24. }
  25. /*
  26. @parem      object      the tbody's object
  27. @parem      object      tr's object (must be null)
  28. @parem      string      the className of onmousedown
  29. @parem      string      the className of onmouseout
  30. */
  31. function order(tt,old,classover,classout) {
  32.     var sf = arguments.callee; //get the function self
  33.     var trs = tt.getElementsByTagName('tr');
  34.     for(var i=0;i<trs.length;i++) {
  35.         trs[i].onmousedown = function () {
  36.             if(this.style.cursor == 'move') {
  37.                 return false;
  38.             }
  39.             classout = this.className;
  40.             this.className = classover;
  41.             this.style.cursor = 'move';
  42.             old = this;
  43.         }
  44.         trs[i].onmouseover = function () {
  45.             if(this.style.cursor == 'move' || !old) {
  46.                 return false;
  47.             }
  48.             var tmp_old = old.cloneNode(true);
  49.             var tmp_now = this.cloneNode(true);
  50.             var p = this.parentNode;
  51.             p.replaceChild(tmp_now,old);
  52.             p.replaceChild(tmp_old,this);
  53.             sf(tt,tmp_old,classover,classout);
  54.         }
  55.         trs[i].onmouseout = function () {
  56.             //this.className = classout;
  57.         }
  58.         trs[i].onmouseup = function () {
  59.             this.className = classout;
  60.             this.style.cursor = '';
  61.             old = null;
  62.         }
  63.     }
  64. }
  65. //-->
  66. </script>
  67. <body>
  68. <table border="0" cellpadding="0" cellspacing="1" class="table">
  69.     <tbody>
  70.         <tr >
  71.             <td>ID</td>
  72.             <td>记录</td>
  73.         </tr>
  74.     </tbody>
  75.     <tbody id="tt">
  76.         <tr >
  77.             <td>1</td>
  78.             <td>记录</td>
  79.         </tr>
  80.         <tr>
  81.             <td>2</td>
  82.             <td>记录</td>
  83.         </tr>
  84.         <tr>
  85.             <td>3</td>
  86.             <td>记录</td>
  87.         </tr>
  88.         <tr>
  89.             <td>4</td>
  90.             <td>记录</td>
  91.         </tr>
  92.     </tbody>
  93. </table>
  94. </body>
  95. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在JavaScript中监听Canvas上的拖拽事件,你需要使用以下步骤: 1. 首先,获取到Canvas元素的引用。可以使用`document.getElementById`或者`document.querySelector`方法来获取Canvas元素的引用。 2. 接下来,为Canvas元素添加`mousedown`、`mousemove`和`mouseup`事件监听器。这些事件将处理鼠标在Canvas上的拖拽操作。 3. 在`mousedown`事件处理函数中,记录鼠标按下时的初始位置(鼠标相对于Canvas的坐标)。 4. 在`mousemove`事件处理函数中,计算鼠标移动时的偏移量,并更新你的绘制逻辑或者其他操作。 5. 最后,在`mouseup`事件处理函数中,清除你所记录的鼠标初始位置,以表示拖拽操作已经结束。 下面是一个简单的示例代码: ```javascript // 获取Canvas元素的引用 const canvas = document.getElementById("myCanvas"); // 定义初始位置和偏移量 let startX, startY; let offsetX = 0, offsetY = 0; // 添加事件监听器 canvas.addEventListener("mousedown", handleMouseDown); canvas.addEventListener("mousemove", handleMouseMove); canvas.addEventListener("mouseup", handleMouseUp); // 鼠标按下事件处理函数 function handleMouseDown(event) { startX = event.clientX - canvas.offsetLeft; startY = event.clientY - canvas.offsetTop; } // 鼠标移动事件处理函数 function handleMouseMove(event) { if (!startX || !startY) return; const currentX = event.clientX - canvas.offsetLeft; const currentY = event.clientY - canvas.offsetTop; offsetX = currentX - startX; offsetY = currentY - startY; // 在这里进行绘制或其他操作 // ... startX = currentX; startY = currentY; } // 鼠标松开事件处理函数 function handleMouseUp() { startX = null; startY = null; } ``` 请注意,上述代码仅为示例,你可能需要根据你的具体需求进行修改和完善。希望对你有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值