未捕获的语法错误:意外的输入结束

问题描述 投票:-1回答:1

我最近创建了一个代码,用于在按下不同的箭头键时调整分度高度。不幸的是,实际上并没有这样做。

我不确定我在这里做错了什么 - 它只告诉我Uncaught Syntax Error:意外的输入结束

代码如下:

<html>
    <head>
    <title>Grow your own div</title>
    <script>
        var high = box.style.offsetHeight;
        var wide = box.style.offsetWidth;
        window.onkeydown = function(){
            if (event.keyCode === 37){
                if (box.style.offsetWidth > 0) {
                    wide = wide--;
                    box.style.offsetWidth = wide;
                }
                else {
                    box.style.offsetWidth = 1;
                }
            }
            else if (event.keyCode === 38) {
                high = high++;
                box.style.offsetHeight = high;
            }
            else if (event.keyCode === 39) {
                wide = wide++;
                box.style.offsetWidth = wide;
            }
            else if (event.keyCode === 40) {
                if (box.style.offsetHeight > 0) {
                    high = high--;
                    box.style.offsetHeight = high;
                }
                else {
                    box.style.offsetHeight = 1;
                }

            }
    </script>
    </head>

    <body>
    <div style="height:100px; width:100px; background-color:orange; position:relative" id="box">   
    </div>
    </body>
</html>

我不熟悉JavaScript或HTML,如果您想了解更多信息,请参阅我的个人资料。

javascript html
1个回答
0
投票

我发现你的代码存在两个问题。我在下面对其进行了格式化,以便更容易阅读。请注意,window.onkeydown()打开的函数未关闭。在结束脚本标记之前,您需要一个额外的大括号。另一个问题是你在身体中有两个div标签,你想要一个div和一个/​​ div。

<html>
  <head>
     <title>Grow your own div</title>
     <script>
          var high = box.style.offsetHeight;
          var wide = box.style.offsetWidth;
          window.onkeydown = function(){
               if (event.keyCode === 37){
                    if (box.style.offsetWidth > 0){
                         wide = wide--;
                         box.style.offsetWidth = wide;
                    }
                    else {
                         box.style.offsetWidth = 1;
                    }
               }
               else if (event.keyCode === 38){
                    high = high++;
                    box.style.offsetHeight = high;
               }
               else if (event.keyCode === 39){
                    wide = wide++;
                    box.style.offsetWidth = wide;
               }
               else if (event.keyCode === 40){
                    if (box.style.offsetHeight > 0){
                         high = high--;
                         box.style.offsetHeight = high;
                    }
                    else {
                         box.style.offsetHeight = 1;
                    }
               }
     </script>
  </head>
  <body>
     <div style="height:100px; width:100px; background-color:orange; position:relative" id="box">
     <div>
  </body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.