无法让 .live on keydown 工作...有什么建议吗?
我尝试使用 .on 代替,并委托,但没有一个起作用。 (代码在不运行时有效) 有人可以帮我吗?我正在循环出一个具有“qspholder”类的列表(通过ajax请求),并且此代码应该允许我使用箭头键选择并单击列表上的项目并输入。然而,由于某种原因,当我尝试实时进行时,我似乎无法使其正常工作。提交和检索值后,我将其(列表)放在带有 .html(data) 的 div 中。该代码不在正在加载的页面上,而是在我将其加载到的页面上。谁能给我一些关于如何让它发挥作用的建议?谢谢。
$(window).live("keydown", function(e){
var liSelected;
var li = $('.qspholder');
$('.qspholder').removeClass('selected');
if(e.which === 40){
if(liSelected){
liSelected.removeClass('selected');
next = liSelected.next();
if(next.length > 0){
liSelected = next.addClass('selected');
}else{
liSelected = li.eq(0).addClass('selected');
}
}else{
liSelected = li.eq(0).addClass('selected');
}
}else if(e.which === 38){
$('.qspholder').removeClass('selected');
if(liSelected){
liSelected.removeClass('selected');
next = liSelected.prev();
if(next.length > 0){
liSelected = next.addClass('selected');
}else{
liSelected = li.last().addClass('selected');
}
}else{
liSelected = li.last().addClass('selected');
}
} else if(e.which === 13) {
liSelected.click();
}
});
I tried using .on instead, and delegate, none of them worked. (code works when not live)
Can anyone give me a hand here? I'm looping out a list (via ajax request) with the class of "qspholder" and this code should allow me to select and click items on the list using the arrow keys and enter. Yet for some reason I just cannot seem to get this working when I try to do it as live. I put it (the list) in a div with .html(data) after submitting and retrieving the values. This code is not on the page it is being loaded from, it's on the page I am loading it to. Can anyone give me some advice as to how I get this working? Thank you.
$(window).live("keydown", function(e){
var liSelected;
var li = $('.qspholder');
$('.qspholder').removeClass('selected');
if(e.which === 40){
if(liSelected){
liSelected.removeClass('selected');
next = liSelected.next();
if(next.length > 0){
liSelected = next.addClass('selected');
}else{
liSelected = li.eq(0).addClass('selected');
}
}else{
liSelected = li.eq(0).addClass('selected');
}
}else if(e.which === 38){
$('.qspholder').removeClass('selected');
if(liSelected){
liSelected.removeClass('selected');
next = liSelected.prev();
if(next.length > 0){
liSelected = next.addClass('selected');
}else{
liSelected = li.last().addClass('selected');
}
}else{
liSelected = li.last().addClass('selected');
}
} else if(e.which === 13) {
liSelected.click();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有效,将其应用到您的代码
http://jsfiddle.net/KBPb4/10/
应用于您的原始代码(还将
IF
更改为SWITCH
)this works, apply it to your code
http://jsfiddle.net/KBPb4/10/
applied to your original code ( also changed the
IF
toSWITCH
)