如何将Onload()设置焦点设置为jQuery Handsontable

时间:2012-06-13 03:45:22

标签: javascript jquery events onload handsontable

在jQuery Handsontable中将焦点onload设置为first字段的正确方法是什么?

以下是示例链接http://jsfiddle.net/4kvMq/4/

$(function () {
  $(window).load(function () {
    $('#example10grid table tbody tr td:first-child').focus();
  });
});

甚至试图手动设置焦点但没有运气!

$('#btnGo').click(function() {
    $(".dataTable table tbody tr td:first-child").focus();
});

很多,非常感谢!

2 个答案:

答案 0 :(得分:2)

感谢您的要求。现在有一种公共方法可以做到这一点。

在按钮上使用它:

$('#btnGo').click(function(event) {
  setTimeout(function() {
      //timeout is needed because Handsontable normally deselects 
      //current cell when you click outside the table
      $("#example10grid").handsontable("selectCell", 0, 0);
  }, 10);
});

或DOM准备好了:

$(function() {
  setTimeout(function() { 
      //timeout is needed because Handsontable normally deselects 
      //current cell when you click outside the table
      $("#example10grid").handsontable("selectCell", 0, 0);
  }, 10);
});

实例:http://jsfiddle.net/warpech/4kvMq/35/

答案 1 :(得分:1)

你不能这样做。问题是握手处理焦点事件的方式。它将单击处理程序绑定到hmtl元素,并在光标位于表上时检测每毫秒。因此,您无法在特定元素上使用click()focus()更改焦点。

此外,由于安全原因无法使用JavaScript更改光标位置(并且jQuery无法本机触发单击或焦点事件),因此无法轻松更改单元格上的焦点。只能修改handontable本身来更改选定的表格单元格。