【问题标题】:How to Onload() set focus to jQuery Handsontable如何 Onload() 将焦点设置为 jQuery Handsontable
【发布时间】:2023-03-21 05:04:01
【问题描述】:

将焦点 onload 设置到 jQuery Handsontable 中的第一个字段的正确方法是什么?

这里是示例链接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();
});

非常感谢!

【问题讨论】:

    标签: javascript jquery events onload handsontable


    【解决方案1】:

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

    在按钮上使用它:

    $('#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/

    【讨论】:

    • 谢谢。这个黑客工作正常。我已经写信给GitHub
    • 现在有一个方法:“selectCell”。我会更新我的答案。
    【解决方案2】:

    你不能这样做。问题在于handsontable 处理焦点事件的方式。它将一个点击处理程序绑定到hmtl 元素,并且如果您的光标在桌子上,则每毫秒检测一次。因此,您不能在特定元素上使用 click()focus() 更改焦点。

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

    【讨论】:

    • 萨米,感谢您的意见。真的很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 2017-05-26
    相关资源
    最近更新 更多