简体   繁体   English

我在文档上使用jquery keydown事件获取了(TypeError jQuery),它引发了很多事件,而不仅仅是选定的事件

[英]I'm getting a (TypeError jQuery) using jquery keydown events on the document, its causing lots of events to fire rather than just the selected one

I'm using a plugin called Vex which is used for dialogs. 我正在使用一个名为Vex的插件,用于对话框。

Vex works by creating dialogs on the fly using JS so the html doesn't exists on the page so for that reason, I used the following code. Vex通过使用JS动态创建对话框来工作,因此html在页面上不存在,因此,我使用了以下代码。 The problem with this is that cause the keydown event is fired on the document, it's causing all keydown events to fire even though I have put the selector in '.test1' 这样做的问题是导致在文档上触发了keydown事件,即使我已将选择器放在“ .test1”中,也会导致所有keydown事件都触发

$(document).keydown('.test1', function(e) {
    //do stuff here
});

Please see the fiddle example here (see the errors logged in the console) 请在此处查看小提琴示例 (请参阅控制台中记录的错误)

Any ideas how to stop this? 有什么想法可以阻止这种情况吗?

Try using .on() if you want to bind dynamic content . 如果要绑定动态内容,请尝试使用.on()。

$(document).on("keydown", ".test1", function(e){
    //do stuff here
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM