我正在尝试在bus_lines div中执行一个按键事件div.
$('#bus_lines').keypress(function(e){
var box = $(this).closest("div:hidden").attr("id");
console.log(box);
});
Run Code Online (Sandbox Code Playgroud)
这是它在firebug中的样子:

它只适用于父div,它是具有bus_lines id的父div.如果我尝试在fs_buslines或fs_bustax中执行keypress事件,它就无法正常工作.我需要知道按键执行的最近div.基本上我正在尝试使用键盘快捷键执行按钮单击事件.
closest搜索元素的祖先.看起来你正在寻找与选择器匹配的当前元素的第一个后代.结果,你想要find
var box = $(this).find("div:hidden:first").attr("id");
Run Code Online (Sandbox Code Playgroud)