zoukankan      html  css  js  c++  java
  • 根据键盘操作表格

      <style type="text/css">
    table
    {
        border-collapse: collapse;
        border: 1px solid blue;
    }
    th, td
    {
        border: 1px solid blue; 
    }
        </style>
    </head>
    <body>
        <h3>根据键盘操作表格!注:IE6sp1 测试可用!FF2 不兼容。</h3>
        tab:正向切换单元格。<br />
        shift + tab:反向切换单元格。<br />
        上下方向键:换行。<br />
        左右方向键:文本框内移动光标。<br />
        <table id="tbeTest">
            <tr>
                <th>姓名</th>
                <th>年龄</th>
                <th>爱好</th>
            </tr>
            <tr>
                <td>
                    <input type="text" style=" 100px;" value="yixianggao" />
                </td>
                <td>
                    <input type="text" style=" 50px; text-align: center;" value="29" />
                </td>
                <td>
                    <input type="text" style=" 150px;" value="Web Development" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" style=" 100px;" />
                </td>
                <td>
                    <input type="text" style=" 50px; text-align: center;" />
                </td>
                <td>
                    <input type="text" style=" 150px;" />
                </td>
            </tr>
        </table>
        <script type="text/javascript">
        <!--
    function $(sId)
    {
        return document.getElementById(sId);
    }

    /*
     * 设置 键盘事件。 
     * 上下键换行、回车增加新行。
     */
    function setCellKeyEvents(oTarget)
    {
        var upKeyCode = 38;
        var downKeyCode = 40;
        var enterKeyCode = 13;

        var oCell;
        for (var i=0; i<oTarget.cells.length; i++)
        {
            oCell = oTarget.cells[i];
            oCell.onkeydown = function()
            {
                switch (event.keyCode)
                {
                    case upKeyCode:
                        moveToPreviousRow(this);
                        break;
                    case downKeyCode:
                        moveToNextRow(this);
                        break;
                    case enterKeyCode:
                        appendNewRow(this);
                        break;
                }
            };
        }
    }
    /*
     * 上移一行。
     */
    function moveToPreviousRow(oTd)
    {
        var startRowIndex = 1;
        var curRowIndex = oTd.parentNode.rowIndex;
        var curCellIndex = oTd.cellIndex;

        if (curRowIndex != startRowIndex)
        {
            oTd.parentNode.previousSibling.cells[curCellIndex].firstChild.focus();
        }
    }
    /*
     * 下移一行。
     */
    function moveToNextRow(oTd)
    {
        var endRowIndex = oTd.parentNode.parentNode.rows.length - 1;
        var curRowIndex = oTd.parentNode.rowIndex;
        var curCellIndex = oTd.cellIndex;

        if (curRowIndex != endRowIndex)
        {
            oTd.parentNode.nextSibling.cells[curCellIndex].firstChild.focus();
        }
    }
    /*
     * 清空文本输入框。
     */
    function clearTextBoxValve(oTarget)
    {
        var cInput = oTarget.getElementsByTagName("input");
        for (var i=0; i<cInput.length; i++)
        {
            if (cInput[i].type.toLowerCase() == "text")
            {
                cInput[i].value = "";
            }
        }
    }
    /*
     * 添加新行。
     */
    function appendNewRow(oTd)
    {
        var endRowIndex = oTd.parentNode.parentNode.rows.length - 1;
        var endCellIndex = oTd.parentNode.cells.length - 1;
        var curRowIndex = oTd.parentNode.rowIndex;
        var curCellIndex = oTd.cellIndex;

        if (curRowIndex == endRowIndex
            && curCellIndex == endCellIndex)
        {
            var lastRow = oTd.parentNode.cloneNode(true);
            clearTextBoxValve(lastRow);
            oTd.parentNode.parentNode.appendChild(lastRow);
            setCellKeyEvents(lastRow);
            lastRow.firstChild.firstChild.focus();
        }
    }

    var oTbe = $("tbeTest");

    setCellKeyEvents(oTbe);

        //-->
        </script>
    </body>

  • 相关阅读:
    Qt 2D绘图之五:图形视图框架的结构和坐标系统
    (转)x264 编码流程
    (转)x264代码详细阅读之x264.c,common.c,encoder.c
    使用ffmepg的lib库调试,debug版本下调试无问题,但release版本会出现跑飞的现象
    (转)视频码率,帧率和分辨率的联系与区别
    (转)windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTime
    (转)x264源码分析(1):main、parse、encode、x264_encoder_open函数代码分析
    转)x264重要结构体详细说明(2): x264_image_t、x264_picture_t、x264_nal_t
    (转)x264重要结构体详细说明(1): x264_param_t
    (转)SCR, PCR, ESCR, PTS, DTS
  • 原文地址:https://www.cnblogs.com/xingfuboke/p/4994778.html
Copyright © 2011-2022 走看看