我想在具有焦点的表行上按键时调用 java 脚本函数。下面是代码,但是当我按 Enter 键时,脚本函数不会被调用。
http://jsfiddle.net/sirishkumar/58FZG/19/
<input id="test" type="text">
<table>
<tr onkeypress="return openLog(e,'row 1')">
<td>Row 1</td>
</tr>
<tr onkeypress="return openLog(e,'row 2')">
<td>Test</td>
</tr>
</table>
var j = jQuery;
var currentRow = 0;
var pagesize = 2;
function ChangeCurrentRow() {
var tableRow = document.getElementsByTagName("tr")[(currentRow % pagesize)];
tableRow.focus();
j(tableRow).siblings().removeClass("highlight-row");
j(tableRow).addClass("highlight-row");
}
j(document).ready(function () {
j('#test').val("Ready");
ChangeCurrentRow();
});
j(document).keydown(function (e) {
if (e.keyCode == 38) {
currentRow--;
ChangeCurrentRow();
return false;
}
if (e.keyCode == 40) {
currentRow++;
ChangeCurrentRow();
return false;
}
});
function openLog(e, id) {
if (e.keyCode == 13) {
$('#input').text(id)
}
return true;
}
最佳答案
行没有“焦点”。您的 focus()
什么也不做。尝试写:
j(tableRow).focus(function(){alert('test');});
tableRow.focus();
你什么也看不到 =)
尝试在 $(document).keypress()
中处理按键事件并对所选行进行操作。 onkeypress
for tr - 不执行任何操作(contenteditable="true"
tr 除外)。
关于javascript - 处理焦点行的按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15290645/