【问题标题】:SCRIPT5007: Unable to get value of the property 'keypress': object is null or undefinedSCRIPT5007:无法获取属性“keypress”的值:对象为空或未定义
【发布时间】:2019-12-31 06:53:47
【问题描述】:

我正在尝试使用如下所示的 jQuery 代码在文本框的按键上执行一些代码,但出现错误。你能帮我吗?我什至尝试过 keydown 但面临同样的问题。

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
    <style>
        textarea.ex1 { background-color: white; width: 610px; height: 310px; overflow: Scroll; }
        #outer {  width:100%; text-align: center;} .inner { display: inline-block; }
        body { background-color: gray; }
    </style>
</head>
<body>
<textarea id="screen" class="ex1"></textarea>
<input type="text" id="command" style="width: 480px; height=600px;"/>
<script>
    $("#command").keydown(function(event){
        alert('Before keycode validation cond');
        var keycode = (event.keyCode ? event.keyCode : event.which);
        alert('After keycode validation');
        if(keycode == '13')
        {
            alert('ENTER key selected');
            $.get('Servlet3', function(response) {
                alert(response);
                appendResponse(response);
            });
            appendCommand();
        }
    });
    function appendCommand() {
        alert('appending command');
        document.getElementById('screen').textContent = document.getElementById('screen').textContent + document.getElementById('command').value;
    }
    function appendResponse(response) {
        alert('appending response');
        document.getElementById('screen').textContent = document.getElementById('screen').textContent + response;
    }
</script>

【问题讨论】:

  • 同时提供您的 HTML
  • 我尝试使用 IE 11 浏览器测试您的代码,发现它运行良好。这是测试结果。 i.postimg.cc/pdvS1kF1/138.gif你能告诉我们你在这个测试中使用的是哪个版本的 iE 吗?另外,请告诉我们您在哪一行收到此错误?它可以帮助缩小问题范围。
  • 我在 IE11 上运行上面的代码并从下面的行收到错误。 $("#command").keydown(function(event){

标签: jquery internet-explorer keypress keydown


【解决方案1】:

看起来您的代码运行良好。检查下面的小提琴。我只是在代码中禁用了 cmets。

function onKeyDown(event) {
        //alert('Before keycode validation cond');
        var keycode = (event.keyCode ? event.keyCode : event.which);
        //alert('After keycode validation');
        if(keycode == '13')
        {
            //alert('ENTER key selected');
            $.get('Servlet3', function(response) {
                //alert(response);
                appendResponse(response);
            });
            appendCommand();
        }
    }
    if (window.addEventListener) {
            window.addEventListener("keydown", onKeyDown, true);
        } else if (document.attachEvent) { // IE 
            // alert(document); You can test this in IE
            document.attachEvent("onkeydown", onKeyDown);
        } else {
            document.addEventListener("keydown", onKeyDown, true);
        }
    function appendCommand() {
        //alert('appending command');
        document.getElementById('screen').textContent = document.getElementById('screen').textContent + document.getElementById('command').value;
    }
    function appendResponse(response) {
        //alert('appending response');
        document.getElementById('screen').textContent = document.getElementById('screen').textContent + response;
    }
textarea.ex1 { background-color: white; width: 610px; height: 310px; overflow: Scroll; }
        #outer {  width:100%; text-align: center;} .inner { display: inline-block; }
        body { background-color: gray; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="screen" class="ex1"></textarea>
<input type="text" id="command" style="width: 480px; height=600px;"/>

【讨论】:

  • 是的,当我尝试从 Eclipse 运行它时它可以工作。但是在 IE 上尝试时出错。
  • 您是指在 Internet Explorer 中吗?您在控制台中遇到什么错误?
  • 是的,仅限 Internet Explorer。下面是错误。 SCRIPT5007:无法获取属性“keypress”的值:对象为 null 或未定义
  • 我已经更新了处理 IE 的答案。请查看我更新的答案。这应该适用于 IE 以及其他浏览器。
  • IE 的keydown 事件存在一个已知问题。
【解决方案2】:

我无法使用 IE 9 浏览器,因此我尝试在 IE 11 浏览器中使用 文档模式 IE 9 进行测试。

您的代码在其中运行良好。这是测试结果。

如果您仍然收到错误,那么我建议您升级到 IE 11 浏览器。 IE 9 版本太旧,目前不在支持范围内。

【讨论】:

    猜你喜欢
    • 2014-02-03
    • 1970-01-01
    • 2013-05-24
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 2014-12-10
    相关资源
    最近更新 更多