2014-09-21 138 views
0

我最近创建了一个代码,用于在按不同的箭头键时调整分隔高度。 不幸的是,它实际上并没有这样做。未捕获的语法错误:意外的输入结束

我不知道我在做什么错在这里 - 它只是告诉我未捕获的语法错误:输入意外结束

的代码如下:

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

+0

请格式化您的代码。 – 2014-09-21 06:08:59

+0

是的,请妥善格式化您的代码,以便我们可以通过您的代码... – 2014-09-21 06:11:25

+0

您的大括号不均衡,如果您格式化您的代码,您将很容易看到它。 – 2014-09-21 06:12:42

回答

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> 
相关问题