If和Else语句错误

If and Else Statements Bug

本文关键字:错误 语句 Else If      更新时间:2023-09-26

我最近一直在学习一些基本的JavaScript,并且遇到了一个问题。我的代码看起来像这样:

<html>
     <body>
          <script type="text/javascript">
                 var name= window.prompt("Type Your Name.")
                 if ( (name=='Ethan') )
                    document.write("You LOVE BACON!!!")
                 else
                     document.write("You Have not entered your name in yet.")
          </script>
     </body>
</html>

我的问题是,当我运行代码并输入我的名字时,页面会显示:

你爱培根!!您还没有输入您的姓名。

else语句和if语句同时出现

<html>
     <body>
          <script type="text/javascript">
                 var name = window.prompt("Type Your Name."); // Assignment!
                 if ( name=='Ethan' )
                    document.write("You LOVE BACON!!!");      // Function Call!
                 else
                     document.write("You Have not entered your name in yet."); // Function Call!
          </script>
     </body>
</html>

分号放在每条语句的末尾,在本例中是在每个函数调用或赋值之后。

try this

if ( name=='Ethan' ) //see here
                    document.write("You LOVE BACON!!!");      // Function Call!
                 else
                     document.write("You Have not entered your name in yet."); //