【问题标题】:How to navigate html table (with tbody) cells by pressing buttons?如何通过按下按钮导航 html 表格(带有 tbody)单元格?
【发布时间】:2017-10-01 22:40:16
【问题描述】:

我有一个带有几个按钮(上、下、右和左)的 html 表格,允许用户通过按每个按钮来导航表格单元格。此时左右按钮根本不起作用,如果您多次按下它,只有向上和向下工作有时会起作用!(如果用户再次单击向下按钮,则向下按钮到达最后一行时,突出显示消失而不是留在最后一行!)。专家能否告诉我如何修复这个损坏的代码。在此先感谢。

注意:当突出显示在单元格中移动时,我希望焦点位于 javascript:doit 上,因为我想添加另一个按钮,以便用户通过单击它调用 doit 函数!n

这是 jsfiddle 中的损坏代码:https://jsfiddle.net/yubeLjnx/

完整代码:

<head>

<style>

table td{
  border:1px solid grey;
  padding:10px;
}

.hilighted {
  border: 2px solid red;
  padding: 1px;
}

button.up {
  margin-left: 2em;
}
button.down {
  margin-left: 1.5em;
}
</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script>
$(document).ready(function() {
  var TableHilighter = function(table, selected) {
    this.table = $(table);
    this.rows = this.table.find('tr').length;
    this.cols = this.table.find('tr').first().find('td').length;
    this.selected = (typeof selected === 'undefined') ? [1, 1] : selected;

    // Hilight our row on initialization
    this.hilight();
  };

  TableHilighter.prototype = {
    "hilight": function(cell) {
      if (typeof cell === 'undefined') {
        cell = this.selected;
      }
      // Clear all hilighted cells
      this.table.find('td').removeClass('hilighted');

      // First find the row, then find the col, and add the .hilighted class
      this.table.find('tr:nth-child(' + this.selected[1] + ')').find('td:nth-child(' + this.selected[0] + ')').addClass('hilighted');
    },
    "move": function(dir) {
      switch (dir) {
        case 'up':
          this._up();
          break;
        case 'down':
          this._down();
          break;
        case 'left':
          this._left();
          break;
        case 'right':
          this._right();
          break;
        default:
          break;
      }
      this.hilight();
      return this.selected;
    },
    "_left": function() {
      if (this._x() > 1) {
        this._x(this._x() - 1);
      }
      return this.selected;
    },
    "_right": function() {
      if (this._x() < this.cols) {
        this._x(this._x() + 1);
      }
      return this.selected;
    },
    "_up": function() {
      if (this._y() > 1) {
        this._y(this._y() - 1);
      }
      return this.selected;
    },
    "_down": function() {
      if (this._y() < this.rows) {
        this._y(this._y() + 1);
      }
      return this.selected;
    },
    "_x": function(x) {
      if (typeof x === 'undefined') {
        return this.selected[0];
      } else {
        this.selected[0] = x;
        return this.selected[0];
      }
    },
    "_y": function(y) {
      if (typeof y === 'undefined') {
        return this.selected[1];
      } else {
        this.selected[1] = y;
        return this.selected[1];
      }
    }
  };

  // Initialize our TableHilighter
  var my_table = new TableHilighter('table');

  // Add button event handlers
  $('.up').on('click', function(e) {
    e.preventDefault();
    my_table.move('up');
  });

  $('.down').on('click', function(e) {
    e.preventDefault();
    my_table.move('down');
  });

  $('.left').on('click', function(e) {
    e.preventDefault();
    my_table.move('left');
  });

  $('.right').on('click', function(e) {
    e.preventDefault();
    my_table.move('right');
  });
});


</script>
</head>

<body>


<button class="up">Up</button>

<div>
    <button class="left">Left</button>
    <button class="right">Right</button>
</div>

<button class="down">Down</button>


<div class="scroller">

<div id="myDiv" style="display: visiable;">  

<table id="demo" style="display: visible;" cellspacing="0" border="1">
<thead>
      <tr>
      <th>Item#</th>
      <th>Logo</th>
      </tr>
</thead>

<tbody>

<tr>
<td class="hilighted">
<div class="image">
<a href="javascript:doit('1')">
<img src="http://mywebsite/images/1/thumb.jpg" alt="">Title 1 
</a>
</div>
</td>



<td>
<div class="image">
<a href="javascript:doit('2')">
<img src="http://mywebsite/images/2/thumb.jpg" alt="">Title 2
</a>
</div>
</td>
</tr>

<tr>
<td><div class="image">
<a href="javascript:doit('3')">
<img src="http://mywebsite/images/3/thumb.jpg"  alt="">Title 3
</a>
</div>
</td>



<td>
<div class="image">
<a href="javascript:doit('4')">
<img src="http://mywebsite/images/4/thumb.jpg" alt="">Title 4
</a>
</div>
</td>
</tr>

<tr>
<td>
<div class="image">
<a href="javascript:doit('5')">
<img src="http://mywebsite/images/5/thumb.jpg" alt="">Title 5
</a>
</div>
</td>



<td>
<div class="image">
<a href="javascript:doit('6')">
<img src="http://mywebsite/images/6/thumb.jpg"  alt="">Title 6
</a>
</div>
</td>
</tr>

</tbody>
</table>

</div>
</div>
</body>

【问题讨论】:

    标签: javascript jquery html-table


    【解决方案1】:

    我已更新您的测试,请参阅评论 :) 我只更改了您的代码中的两行

    $(document).ready(function() {
      var TableHilighter = function(table, selected) {
        this.table = $(table) 
        this.rows = this.table.find("tbody").find('tr').length ;
        this.cols = this.table.find("tbody").find('tr').first().find('td').length ;// thead dose not containe td thats why you get -1 on cols :)
        this.selected = (typeof selected === 'undefined') ? [1, 1] : selected;
    
        // Hilight our row on initialization
        this.hilight();
      };
    
      TableHilighter.prototype = {
        "hilight": function(cell) {
          if (typeof cell === 'undefined') {
            cell = this.selected;
          }
          // Clear all hilighted cells
          this.table.find('td').removeClass('hilighted');
          
          // First find the row, then find the col, and add the .hilighted class
          this.table.find('tr:nth-child(' + this.selected[1] + ')').find('td:nth-child(' + this.selected[0] + ')').addClass('hilighted');
        },
        "move": function(dir, v) {
          switch (dir) {
            case 'up':
              this._up();
              break;
            case 'down':
              this._down();
              break;
            case 'left':
              this._left();
              break;
            case 'right':
              this._right();
              break;
            case 'doit':
             this._doit(v);
             break;
            default:
              break;
          }
          this.hilight();
          return this.selected;
        },
        "_doit" : function(selected){
    
          // here i found the selected cell and row do what you want with them
         alert("value is " + selected);
        
        }, 
        "_left": function() {
          if (this._x() > 1) {
            this._x(this._x() - 1);
          }
          return this.selected;
        },
        "_right": function() {
          if (this._x() < this.cols) {
            this._x(this._x() + 1);
          }
          return this.selected;
        },
        "_up": function() {
          if (this._y() > 1) {
            this._y(this._y() - 1);
          }
          return this.selected;
        },
        "_down": function() {
          if (this._y() < this.rows) {
            this._y(this._y() + 1);
          }
          return this.selected;
        },
        "_x": function(x) {
          if (typeof x === 'undefined') {
            return this.selected[0];
          } else {
            this.selected[0] = x;
            return this.selected[0];
          }
        },
        "_y": function(y) {
          if (typeof y === 'undefined') {
            return this.selected[1];
          } else {
            this.selected[1] = y;
            return this.selected[1];
          }
        }
      };
      
      // Initialize our TableHilighter
      var my_table = new TableHilighter('table');
      
      // Add button event handlers
      $('.up').on('click', function(e) {
        e.preventDefault();
        my_table.move('up');
      });
      
      $('.down').on('click', function(e) {
        e.preventDefault();
        my_table.move('down');
      });
      
      $('.left').on('click', function(e) {
        e.preventDefault();
        my_table.move('left');
      });
      
      $('.right').on('click', function(e) {
        e.preventDefault();
        my_table.move('right');
      });
      
      $('.doit').on('click', function(e) {
        e.preventDefault();
         var selectedCel = my_table.table.find("tbody").find('td.hilighted');
         var selectedRow = selectedCel.parent();
         var row = selectedRow.index();
         var cell = selectedCel.index();
         var value = ((row * selectedRow.find("td").length) + cell) +1;
         
        my_table.move('doit', value);
      });
      
    });
    table td{
      border:1px solid grey;
      padding:10px;
    }
    
    .hilighted {
      border: 2px solid red;
      padding: 1px;
    }
    
    button.up {
      margin-left: 2em;
    }
    button.down {
      margin-left: 1.5em;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button class="up">Up</button>
    
    <div>
        <button class="left">Left</button>
        <button class="right">Right</button>
    </div>
    
    <button class="down">Down</button>
    
    <button class='doit'>click highlighed cell</button>
    
    <div class="scroller">
    
    <div id="myDiv" style="display: visiable;">  
    
    <table id="demo" style="display: visible;" cellspacing="0" border="1">
    
    <thead>
          <tr>
          <th>Item#</th>
          <th>Logo</th>
          </tr>
    </thead>
      
    <tbody>
    
    <tr>
    <td class="hilighted">
    <div class="image">
    <a href="javascript:doit('1')">
    <img src="http://mywebsite/images/1/thumb.jpg" alt="">Title 1 
    </a>
    </div>
    </td>
    
    
    
    <td>
    <div class="image">
    <a href="javascript:doit('2')">
    <img src="http://mywebsite/images/2/thumb.jpg" alt="">Title 2
    </a>
    </div>
    </td>
    </tr>
    
    <tr>
    <td><div class="image">
    <a href="javascript:doit('3')">
    <img src="http://mywebsite/images/3/thumb.jpg"  alt="">Title 3
    </a>
    </div>
    </td>
    
    
    
    <td>
    <div class="image">
    <a href="javascript:doit('4')">
    <img src="http://mywebsite/images/4/thumb.jpg" alt="">Title 4
    </a>
    </div>
    </td>
    </tr>
    
    <tr>
    <td>
    <div class="image">
    <a href="javascript:doit('5')">
    <img src="http://mywebsite/images/5/thumb.jpg" alt="">Title 5
    </a>
    </div>
    </td>
    
    
    
    <td>
    <div class="image">
    <a href="javascript:doit('6')">
    <img src="http://mywebsite/images/6/thumb.jpg"  alt="">Title 6
    </a>
    </div>
    </td>
    </tr>
    
    </tbody>
    </table>
    
    </div>
    </div>

    【讨论】:

    • 感谢您提及需要更正哪些行。我计划添加另一个名为“单击高亮单元格”的按钮,因此一旦用户单击该按钮,它应该在该单元格上触发 javascript:doit 吗?我怎样才能做到这一点?
    • 谢谢。是否可以通过 doit 函数获取 doit 的值?( doit('4') )
    • 我已经更新了我的答案 :) 我计算了这个值,而不是从链接中获取它。 var value = ((row * selectedRow.find("td").length) + cell) +1;
    【解决方案2】:

    您的代码中的问题是您得到了 tr (4) 和 td (6) 的总和,并且当您每次加 1 时,这不会停止。

    尝试以下方法:

    $(document).ready(function() {
      var TableHilighter = function(table, selected) {
        this.table = $(table);
        this.rows = $( "table tr" ).length;
        this.cols = $( "table tr td" ).length;
        this.selected = (typeof selected === 'undefined') ? [1, 1] : selected;
        
        // Hilight our row on initialization
        this.hilight();
      };
    
      TableHilighter.prototype = {
        "hilight": function(cell) {
          if (typeof cell === 'undefined') {
            cell = this.selected;
          }
          // Clear all hilighted cells
          this.table.find('td').removeClass('hilighted');
          
          // First find the row, then find the col, and add the .hilighted class
          this.table.find('tr:nth-child(' + this.selected[1] + ')').find('td:nth-child(' + this.selected[0] + ')').addClass('hilighted');
        },
        "move": function(dir) {
          switch (dir) {
            case 'up':
              this._up();
              break;
            case 'down':
              this._down();
              break;
            case 'left':
              this._left();
              break;
            case 'right':
              this._right();
              break;
            default:
              break;
          }
          this.hilight();
          return this.selected;
        },
        "_left": function() {
          if (this._x() > 1) {
            this._x(this._x() - 1);
          }
          return this.selected;
        },
        "_right": function() {
          if (this._x() < (this.cols-4)) {
          
            this._x(this._x() + 1);
            }
          return this.selected;
        },
        "_up": function() {
          if (this._y() > 1) {
            this._y(this._y() - 1);
          }
          return this.selected;
        },
        "_down": function() {
          if (this._y() < (this.rows-1)) {
         
            this._y(this._y() + 1);
          }
          return this.selected;
        },
        "_x": function(x) {
          if (typeof x === 'undefined') {
            return this.selected[0];
          } else {
            this.selected[0] = x;
            return this.selected[0];
          }
        },
        "_y": function(y) {
          if (typeof y === 'undefined') {
            return this.selected[1];
          } else {
            this.selected[1] = y;
            return this.selected[1];
          }
        }
      };
      
      // Initialize our TableHilighter
      var my_table = new TableHilighter('table');
      
      // Add button event handlers
      $('.up').on('click', function(e) {
        e.preventDefault();
        my_table.move('up');
      });
      
      $('.down').on('click', function(e) {
        e.preventDefault();
        my_table.move('down');
      });
      
      $('.left').on('click', function(e) {
        e.preventDefault();
        my_table.move('left');
      });
      
      $('.right').on('click', function(e) {
        e.preventDefault();
        my_table.move('right');
      });
    });
    table td{
      border:1px solid grey;
      padding:10px;
    }
    .hilighted {
      border: 2px solid red;
      padding: 1px;
    }
    
    button.up {
      margin-left: 2em;
    }
    button.down {
      margin-left: 1.5em;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <button class="up">Up</button>
    
    <div>
        <button class="left">Left</button>
        <button class="right">Right</button>
    </div>
    
    <button class="down">Down</button>
    
    <div class="scroller">
    
    <div id="myDiv" style="display: visiable;">  
    
    <table id="demo" style="display: visible;" cellspacing="0" border="1">
    
    <thead>
          <tr>
          <th>Item#</th>
          <th>Logo</th>
          </tr>
    </thead>
      
    <tbody>
    
    <tr>
    <td class="hilighted">
    <div class="image">
    <a href="javascript:doit('1')">
    <img src="http://mywebsite/images/1/thumb.jpg" alt="">Title 1 
    </a>
    </div>
    </td>
    
    
    
    <td>
    <div class="image">
    <a href="javascript:doit('2')">
    <img src="http://mywebsite/images/2/thumb.jpg" alt="">Title 2
    </a>
    </div>
    </td>
    </tr>
    
    <tr>
    <td><div class="image">
    <a href="javascript:doit('3')">
    <img src="http://mywebsite/images/3/thumb.jpg"  alt="">Title 3
    </a>
    </div>
    </td>
    
    
    
    <td>
    <div class="image">
    <a href="javascript:doit('4')">
    <img src="http://mywebsite/images/4/thumb.jpg" alt="">Title 4
    </a>
    </div>
    </td>
    </tr>
    
    <tr>
    <td>
    <div class="image">
    <a href="javascript:doit('5')">
    <img src="http://mywebsite/images/5/thumb.jpg" alt="">Title 5
    </a>
    </div>
    </td>
    
    
    
    <td>
    <div class="image">
    <a href="javascript:doit('6')">
    <img src="http://mywebsite/images/6/thumb.jpg"  alt="">Title 6
    </a>
    </div>
    </td>
    </tr>
    
    </tbody>
    </table>
    
    </div>
    </div>

    【讨论】:

    • 非常感谢您的回复。 @Mohammed Elhag。您的更正适用于短样本,但由于我正在使用来自 ajax(300 个单元格)的数据填充表格,并且在 $(document).ready(function() 运行时表格尚未完全填充,这会导致按钮效果不好(第一个单元格也无法突出显示)!如何解决这个问题。此外,我计划添加另一个名为“单击突出显示的单元格”的按钮,因此一旦用户单击该按钮,它应该触发 javascript:doit 在该单元格上? 我怎样才能做到这一点?
    猜你喜欢
    • 2020-02-04
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多