原型:在文档上按下键,在表单中停止观察
我正在尝试使用ajax左/右箭头键来制作下一个/上一个页面(就像在facebook上一样)。一切都很好并且有效,但我的页面上也有表单,如果用户专注于该表单,我想停止观察左/右键,因为没有它,您将无法使用表单中的箭头移动,您将得到下一个/先前的图像而不是形式中的运动。
我的实际代码在这里,comment_name是字段的id,如果我第一次点击该字段,那么对keydown的观察就会停止。但是,如果我先按左/右键,然后单击表单并不会停止观察。
在我看来,它不能与 ajax 返回的 html 一起使用...?
document.observe("dom:loaded", function() { document.observe('keydown', function(event){ if(event.keyCode == Event.KEY_RIGHT) { if (Prototype.Browser.IE) { $('next').click();; } else { $('next').simulate('click'); } } }); $('comment_name').observe('focus', function(event){ document.stopObserving('keydown'); });
页面在这里 http://beta.sigut.net/photos/2027,当前代码在 javascripts/application.js 中
非常感谢!也许我的方法是错误的......
I am trying to make next/previous page with ajax left/right arrows keys working (like on facebook). Everything is fine and works, but I also have form on my page and if user focus on that form I want to stop observing left/right key, because without that you can't move with arrows in the form, you are getting next/previous image instead of movement in the form.
My actual code is here, the comment_name is id of the field, if I first click into the field then the observing of the keydown is stopped. But if I first press left/right key then clicking into form does not stop observing.
It seems to me like it can't work with ajax-returned html...?
document.observe("dom:loaded", function() { document.observe('keydown', function(event){ if(event.keyCode == Event.KEY_RIGHT) { if (Prototype.Browser.IE) { $('next').click();; } else { $('next').simulate('click'); } } }); $('comment_name').observe('focus', function(event){ document.stopObserving('keydown'); });
The page is here http://beta.sigut.net/photos/2027, the current code in javascripts/application.js
Thank you very much! maybe my approach is all wrong...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次加载新的 AJAX 内容时,您都必须绑定观察。
这意味着
在将 AJAX 加载的 html 添加到 DOM 中之后调用。
You have to bind the observe every time new AJAX content is loaded.
That means calling
After adding the AJAX loaded html into the DOM.