javascript 如何使用javascript聚焦表格单元格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13858611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:59:53  来源:igfitidea点击:

how to focus a table cell using javascript?

javascriptjquery

提问by Madura Harshana

VB script statement ,

VB脚本语句,

Set oHighlightedRow = document.all("SearchRow" & nHighlightedRow)oHighlightedRow.cells(0).focus()

Set oHighlightedRow = document.all("SearchRow" & nHighlightedRow)oHighlightedRow.cells(0).focus()

These two statments need to be converted to javascript.anyone can help me to find a solution? Thanx

这两个语句需要转换成javascript。谁能帮我找到解决办法?谢谢

My converted code was,

我转换后的代码是,

var oHighlightedRow = $("#SearchRow" + nHighlightedRow);
oHighlightedRow.cells[0].focus();

Is this correct ?

这个对吗 ?

回答by nnnnnn

OK:

行:

var oHighlightedRow = document.all("SearchRow" + nHighlightedRow);
oHighlightedRow.cells[0].focus();

Or, better (assuming the row has an id of "SearchRow" + nHighlightedRow):

或者,更好(假设该行的 id 为"SearchRow" + nHighlightedRow):

var oHighlightedRow = document.getElementById("SearchRow" + nHighlightedRow);
oHighlightedRow.cells[0].focus();

Or, jQuery (again assuming the row has an id of "SearchRow" + nHighlightedRow):

或者,jQuery(再次假设该行的 id 为"SearchRow" + nHighlightedRow):

$("#SearchRow" + nHighlightedRow + " td:first").focus();

回答by Pavel Nikolov

You cannot focus table cells on all browsers. Here is what the jQuery documentation says:

您无法将表格单元格聚焦在所有浏览器上。这是 jQuery 文档所说的:

The focus event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (<input>, <select>, etc.) and links (<a href>). In recent browser versions, the event can be extended to include all element types by explicitly setting the element's tabindex property. An element can gain focus via keyboard commands, such as the Tab key, or by mouse clicks on the element.

The focus event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (<input>, <select>, etc.) and links (<a href>). In recent browser versions, the event can be extended to include all element types by explicitly setting the element's tabindex property. An element can gain focus via keyboard commands, such as the Tab key, or by mouse clicks on the element.

To make sure this works with all browsers you can implement some CSS class and add event listeners for mouse keys. Then just add/remove the css classes from table cells.

为了确保这适用于所有浏览器,您可以实现一些 CSS 类并为鼠标键添加事件侦听器。然后只需从表格单元格中添加/删除 css 类。

in order to focus an element with id="target"use this

为了id="target"使用 this来聚焦元素

$('#target').focus();

回答by Murali Murugesan

You can do this simply with the help of jQuery library with simple lines

您可以通过简单的线条借助 jQuery 库轻松完成此操作

$('selector').focus();                     

// $('selector') --> could be $('#tableId td')  

For more information please check http://api.jquery.com/category/selectors/

有关更多信息,请查看http://api.jquery.com/category/selectors/