如何通过按键使用 jquery .live() ?

发布于 2024-09-27 02:00:01 字数 741 浏览 6 评论 0原文

我正在为客户制作一个摄影作品集,并决定使其更具互动性。我去掉了网站上的所有按钮,并让用户使用击键与网站进行交互。我的原始代码使用:

$(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪眸﹌ 2024-10-04 02:00:01

我认为问题不在于绑定事件的方式。

在事件处理程序中,您可以执行以下操作:

var imgwidth = $('#gallery img').attr('width');

这将为您提供第一个图像的宽度(请参阅 attr 文档)。

如何确定用户正在与哪张图像进行交互?如果它有焦点,那么你可以这样做

$('#gallery img').live("keydown", function(e) {
  // here, 'this' is the DOM image object, and $(this) is the jQuery object for it
  // ...
});

……但关键是,你需要某种方法让计算机确定用户打算与哪个图像进行交互。

I don't think the problem is with the way you are binding the events.

Inside your event handler, you do for example:

var imgwidth = $('#gallery img').attr('width');

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.

$('#gallery img').live("keydown", function(e) {
  // here, 'this' is the DOM image object, and $(this) is the jQuery object for it
  // ...
});

...but the point is, you need some way of letting the computer determine which image the user intends to interact with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文