javascript 防止输入字段上的点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12181359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent click-Event on input field
提问by flecki89
I am developing a PhoneGap-App for iPad. On one screen you have to fill out a form with about 20 textfields. As input-fields only react to the click-event (which have this not long but yet annoying delay) I try the following
我正在为 iPad 开发 PhoneGap-App。在一个屏幕上,您必须填写一个包含大约 20 个文本字段的表单。由于输入字段只对点击事件做出反应(这不长但很烦人的延迟)我尝试以下
$('input[type="text"], input[type=number], input[type=date], input[type="tel"], input[type=password], input[type="email"], input[type="url"], textarea, select').live("touchend", function(e) {
if(!swipe) {
$(this).focus();
}
swipe = false;
return false;
});
(I check for swipe in the touchmove-event)
(我在 touchmove 事件中检查滑动)
This works, but now I want to prevent the original click event on the inputs. The problem is, when I activate an input-field with the .focus() method, the keyboard pops up and slides the page a little bit up AND then the click-Event gets fired and activates another input-field a little bit below my desired input.
这有效,但现在我想阻止输入上的原始点击事件。问题是,当我使用 .focus() 方法激活输入字段时,键盘会弹出并将页面向上滑动一点,然后点击事件被触发并激活另一个输入字段低于我的所需的输入。
For preventing click I already tried
为了防止点击我已经尝试过
$('input[type="text"], input[type=number], input[type=date], input[type="tel"], input[type=password], input[type="email"], input[type="url"], textarea, select').live("click", function(e) {
return false;
});
but this also doesn't work :(
但这也不起作用:(
Is there another trick to activate input-fields immediately after I touched it without any delay?
是否有其他技巧可以在我毫不拖延地触摸它后立即激活输入字段?
回答by alessio271288
you can try this
你可以试试这个
$('input, textarea, select').click(function(event) {
event.preventDefault();
});
回答by insomiac
You need to use preventDefault to prevent the default action:
您需要使用 preventDefault 来阻止默认操作:
$('input[type="text"], input[type=number], input[type=date], input[type="tel"], input[type=password], input[type="email"], input[type="url"], textarea, select').live("click", function(e) {
e.preventDefault();
});
Documentation : http://api.jquery.com/event.preventDefault/
回答by ymutlu
It is stop propagation.
它是停止传播。
$('input[type="text"], input[type=number], input[type=date], input[type="tel"], input[type=password], input[type="email"], input[type="url"], textarea, select').live("click", function(event) {
event.stopPropagation();
});
回答by Austin Marino
I had this same issue as you and tried to implement all of the other solutions posted and found that none of them completely worked for me. Instead, I borrowed ideas from all of the prior solutions and found that this snippet of code solved my problem.
我和你有同样的问题,并试图实施所有其他发布的解决方案,但发现它们中没有一个完全适合我。相反,我借鉴了之前所有解决方案的想法,发现这段代码解决了我的问题。
$('input, textarea, select').click(function (event) {
event.stopPropagation();
});