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

Uncaught Syntax Error: Unexpected end of input

本文关键字:输入 意外 结束 错误 语法      更新时间:2023-09-26

我最近创建了一个代码,当您按下不同的箭头键时,该代码可以调整分割高度。不幸的是,它实际上并没有这么做。

我不确定我在这里做错了什么-它只是告诉我未经编辑的语法错误:输入的意外结束

代码如下:

<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,如果你想了解更多信息,请参阅我的个人资料。

我发现您的代码有两个问题。我在下面格式化了它,以便更容易阅读。请注意,在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>