jQuery JavaScript 中是否存在与 preventDefault() 相反的函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1688567/
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
Is there an opposite function of preventDefault() in JavaScript?
提问by Wilkins
I am counting words in a text field and after a certain amount of words, I use prevent default. In the else, I would like to re-enable the default commands.
我正在计算文本字段中的单词,并且在一定数量的单词之后,我使用防止默认值。在 else 中,我想重新启用默认命令。
Does preventDefault(
) have an opposite function?
确实preventDefault(
)有一个相反的功能?
Here is some sample code:
下面是一些示例代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>preventDefault</title>
<style type="text/css"></style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var wordNum = 3;
var input_length;
$("#max").text("max word(s): " + wordNum);
$("#test").keypress(function(event) {
input_length = $.trim($(this).val())
.replace(/\s+/g, " ")
.split(' ').length;
if (input_length > (wordNum - 1)) {
event.preventDefault();
} else {
return true;
}
});
});
</script>
</head>
<body>
<div id="max"></div>
<textarea id="test" cols="20" rows="5"></textarea>
</body>
</html>
It seems to work on IE, but Firefox doesn't like it.
它似乎适用于 IE,但 Firefox 不喜欢它。
采纳答案by Pickle
I think the problem you are having is that you are not looking for the delete key. Preventdefault does not cancel the event handler all together. But with your code once you hit the maximum length of your field the user will no longer be able delete any characters because the delete keypress is being cancelled.
我认为您遇到的问题是您没有在寻找删除键。preventdefault 不会一起取消事件处理程序。但是使用您的代码,一旦您达到字段的最大长度,用户将无法再删除任何字符,因为删除按键被取消。
The reason it works in IE is because IE does not fire the keypress event for delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab. In Safari the keycode for delete is incorrect in the keypress event.
它在 IE 中工作的原因是因为 IE 不会触发删除、结束、输入、转义、功能键、主页、插入、翻页/向下和选项卡的按键事件。在 Safari 中,keypress 事件中用于删除的键码不正确。
For these reasons I have a twofold suggestion; first, use the keydown event instead so that you get the correct keycodes.
出于这些原因,我有双重建议;首先,改用 keydown 事件,以便获得正确的键码。
Second, look at the keycode and if it is delete or backspace then don't preventDefault.
其次,查看键码,如果它是删除或退格键,则不要阻止默认值。
if ((event.keyCode != 46 && event.keyCode != 8) || input_length > (wordNum - 1)) {
return false;
} else {
return true;
}
回答by Les
Shouldn't
不应该
if maxwords
preventDefault
else
return true
do the trick ?
做的伎俩?
回答by Tzury Bar Yochay
simply use return true;
at the end of your eventHandler. That would bubble up the event to the browser.
只需return true;
在 eventHandler 的末尾使用。这会将事件冒泡到浏览器。
回答by Kobi
Doesn't look like it, https://developer.mozilla.org/en/DOM/event, but you can just not call it...
看起来不像,https://developer.mozilla.org/en/DOM/event,但你不能称之为......
回答by Jon
$("#delete_button").click(function(e){
var category = $("#category").val();
var answer = confirm("Are you sure you would like to delete the " + category + " category?");
if (!answer)
e.preventDefault();
});