如何通过按键使用 jquery .live() ?
我正在为客户制作一个摄影作品集,并决定使其更具互动性。我去掉了网站上的所有按钮,并让用户使用击键与网站进行交互。我的原始代码使用:
$(document).bind('keydown', function( e ) {
但不幸的是,这不允许用户与通过 jquery 加载的图片进行交互。因此访问者只能与第一张图片进行交互。我环顾四周,发现 .live() 方法应该将事件绑定到所有对象,无论是在文档加载时还是在文档加载后加载。但由于某种原因,它不适用于我的代码。我正在使用 jquery 1.4.2,这是我的代码示例:
$(document).live('keydown', function( e ) {
if (e.keyCode == 32) {
var imgwidth = $('#gallery img').attr('width');
if(imgwidth == 640) {
$('#content div#image').removeClass('large');
$('#content img').removeClass('large');
}else{
$('#content div#image').addClass('large');
$('#content img').addClass('large');
}
return false;
};
});
任何帮助将不胜感激!
I am building a photography portfolio for a client and decided to make it a little bit more interactive. I got rid of all buttons on the site and had the user interact with the site using key strokes. My original code used:
$(document).bind('keydown', function( e ) {
but this unfortunately would not allow the user to interact with the pictures that were loaded via jquery. so the visitor could only interact with the first picture. I looked around and found that the .live() method was supposed to bind an event to all objects whether loaded when the document loads, or after the fact. But for some reason it does not work with my code. I am using jquery 1.4.2 and this is a sample piece of my code:
$(document).live('keydown', function( e ) {
if (e.keyCode == 32) {
var imgwidth = $('#gallery img').attr('width');
if(imgwidth == 640) {
$('#content div#image').removeClass('large');
$('#content img').removeClass('large');
}else{
$('#content div#image').addClass('large');
$('#content img').addClass('large');
}
return false;
};
});
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题不在于绑定事件的方式。
在事件处理程序中,您可以执行以下操作:
这将为您提供第一个图像的宽度(请参阅 attr 文档)。
如何确定用户正在与哪张图像进行交互?如果它有焦点,那么你可以这样做
……但关键是,你需要某种方法让计算机确定用户打算与哪个图像进行交互。
I don't think the problem is with the way you are binding the events.
Inside your event handler, you do for example:
This will give you the width of the first image (see the attr docs).
How do you determine which image the user is interacting with? If it has focus, then you can do e.g.
...but the point is, you need some way of letting the computer determine which image the user intends to interact with.