[英]jQuery Prevent Default - doesn't appear to work in some circumstances
I have used event.preventDefault()
and event.stopPropagation()
in various places but it always seems to be a bit of a struggle to get it to work. 我在各个地方都使用了event.preventDefault()
和event.stopPropagation()
,但是要使其正常工作似乎总是有些困难。
I have now hit a wall with the instance:- 我现在用实例撞墙了:-
$("#existing_Flavours").on("keydown", function(event){
switch(event.which){
case 9:
//tab called permit
break;
default:
event.preventDefault();
event.stopPropagation();
} // end switch
});
I want the tab key to work but nothing else. 我希望Tab键起作用,但别无其他。
Been round this endlessly and no matter what I cannot stop normal key strokes from occurring. 无休止地绕着它转动,无论我无法阻止正常的击键发生。
If the element is dynamic you'll need to delegate the event to an element that actually exists when binding the keydown : 如果元素是动态的,则需要将事件委托给绑定keydown时实际存在的元素:
$(document).on("keydown", "#existing_Flavours", function(event){
switch(event.which){
case 9:
//tab called permit
break;
default:
//event.preventDefault();
//event.stopPropagation();
// or just use return false
return false;
}
});
My guess is it has to do with which events actually cause a character to appear. 我的猜测是与哪些事件实际导致角色出现有关。 I would try binding to keydown keyup keypress
to make sure you catch all of them. 我会尝试绑定到keydown keyup keypress
,以确保您抓住了所有它们。
well, first of all, i dont like the switch to much (probably this has nothing to do), try: 好吧,首先,我不喜欢切换到太多(可能与此无关),请尝试:
$("#existing_Flavours").live("keydown", function(event){
var keyCode = event.keyCode || event.which;
if (keyCode != 9) {
event.preventDefault();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.