JavaScript 中是否有与 PreventDefault() 相反的函数?

发布于 2024-08-10 18:31:40 字数 1464 浏览 4 评论 0原文

我正在计算文本字段中的单词数,并且在达到一定数量的单词后,我使用阻止默认值。在其他情况下,我想重新启用默认命令。

preventDefault() 是否有相反的功能?

这是一些示例代码:

<!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>

它似乎可以在 IE 上运行,但 Firefox 不喜欢它。

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.

Does preventDefault() have an opposite function?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

颜漓半夏 2024-08-17 18:31:40

我认为您遇到的问题是您没有寻找删除键。 Preventdefault 不会一起取消事件处理程序。但是,使用您的代码,一旦达到字段的最大长度,用户将无法再删除任何字符,因为删除按键已被取消。

它在 IE 中工作的原因是因为 IE 不会触发删除、结束、输入、转义、功能键、home、插入、pageUp/Down 和 tab 的按键事件。在 Safari 中,按键事件中用于删除的键码不正确。

出于这些原因,我有一个双重建议:首先,使用 keydown 事件,以便获得正确的键码。

其次,查看键码,如果是删除或退格键,则不要阻止默认。

if ((event.keyCode != 46 && event.keyCode != 8) || input_length > (wordNum - 1)) {
    return false;
} else {
    return true;
}

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.

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.

For these reasons I have a twofold suggestion; first, use the keydown event instead so that you get the correct keycodes.

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;
}
旧伤还要旧人安 2024-08-17 18:31:40

不应该

if maxwords
   preventDefault
else
   return true

做这个伎俩吗?

Shouldn't

if maxwords
   preventDefault
else
   return true

do the trick ?

风和你 2024-08-17 18:31:40

只需在 eventHandler 末尾使用 return true; 即可。这会将事件冒泡到浏览器。

simply use return true; at the end of your eventHandler. That would bubble up the event to the browser.

三寸金莲 2024-08-17 18:31:40

看起来不像,https://developer.mozilla.org/en/DOM/event ,但你可以不调用它...

Doesn't look like it, https://developer.mozilla.org/en/DOM/event , but you can just not call it...

桃扇骨 2024-08-17 18:31:40
$("#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();
        });
$("#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();
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文