2015-09-27 102 views
-1

我有一個按鈕,單擊時會彈出一個警報窗口。點擊按鈕後,名爲「master」的變量將從false更改爲true。然後,我有一個if語句來檢查變量是否爲真。如果是,則它會提醒變量的值。這是問題:最後一部分沒有發生。這就像if語句沒有執行,然後停止了程序的其餘部分。我確實檢查了「主」變量是否註冊爲真,並且是,所以我真的不知道什麼是錯的。JavaScript布爾如果語句問題

  <!DOCTYPE html> 
      <html> 
      <head> 
       <title>Tic-Tac-Toe Game</title> 
       <!-- Latest compiled and minified CSS --> 
       <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 

       <!-- Optional theme --> 
       <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> 
       <style type="text/css"> 
        #buttons{ 
         margin-top: 50px; 
        } 
        #x{ 
         margin-left: 10px; 
        } 
        #table{ 
         margin-top: 15px; 
         background: gray; 
         table-layout: fixed; 
         width: auto; 
         width: 250px; 
         height: 250px; 


        } 


       </style> 
      </head> 
      <body> 
       <div class="container" id="buttons">  
        <h2 id = "h2" >Choose your team!</h2> 
        <button onclick = "myFunctions()" type = "button" class="btn btn-default"><span style = "font-weight: bold; font-size: 110%;">O</span></button> 
        <button type = "button" id = "x" class="btn btn-default"><span class = "glyphicon glyphicon-remove"></span></button> 

        <table id = "table" class="table table-bordered"> 
         <tbody> 
          <tr> 
           <td id = "td" onclick="myFunction()"></td> 
           <td onclick="myFunction()"></td> 
           <td onclick="myFunction()"></td> 
          </tr> 
          <tr> 
           <td onclick="myFunction()"></td> 
           <td onclick="myFunction()"></td> 
           <td onclick="myFunction()"></td> 
          </tr> 
          <tr> 
           <td onclick="myFunction()"></td> 
           <td onclick="myFunction()"></td> 
           <td onclick="myFunction()"></td> 
          </tr> 
         </tbody> 
        </table> 


       </div> 


       <script type="text/javascript"> 
        var master = false; 
        var servant = false; 
        function myFunction(){ 
         alert('hi'); 
        } 

        function myFunctions(){ 
         master = true; 
        } 

        if (master == true) { 
         alert(master); 
        } 

       </script> 

       <!-- Latest compiled and minified JavaScript --> 
       <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
      </body> 
      </html> 
+0

請爲您的函數選擇更好的名稱。這是你問題的重要部分。 –

回答

1

請參閱下面更正的代碼。

<!DOCTYPE html> 
    <html> 
    <head> 
     <title>Tic-Tac-Toe Game</title> 
     <!-- Latest compiled and minified CSS --> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 

     <!-- Optional theme --> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> 
     <style type="text/css"> 
      #buttons{ 
       margin-top: 50px; 
      } 
      #x{ 
       margin-left: 10px; 
      } 
      #table{ 
       margin-top: 15px; 
       background: gray; 
       table-layout: fixed; 
       width: auto; 
       width: 250px; 
       height: 250px; 


      } 


     </style> 
    </head> 
    <body> 
     <div class="container" id="buttons">  
      <h2 id = "h2" >Choose your team!</h2> 
      <button onclick = "myFunctions()" type = "button" class="btn btn-default"><span style = "font-weight: bold; font-size: 110%;">O</span></button> 
      <button type = "button" id = "x" class="btn btn-default"><span class = "glyphicon glyphicon-remove"></span></button> 

      <table id = "table" class="table table-bordered"> 
       <tbody> 
        <tr> 
         <td id = "td" onclick="myFunction()"></td> 
         <td onclick="myFunction()"></td> 
         <td onclick="myFunction()"></td> 
        </tr> 
        <tr> 
         <td onclick="myFunction()"></td> 
         <td onclick="myFunction()"></td> 
         <td onclick="myFunction()"></td> 
        </tr> 
        <tr> 
         <td onclick="myFunction()"></td> 
         <td onclick="myFunction()"></td> 
         <td onclick="myFunction()"></td> 
        </tr> 
       </tbody> 
      </table> 


     </div> 


     <script type="text/javascript"> 
      var master = false; 
      var servant = false; 
      function myFunction(){ 
       alert('hi'); 
       master = true; 
      if (master == true) { 
       alert(master); 
      } 
      } 
     </script> 

     <!-- Latest compiled and minified JavaScript --> 
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
    </body> 
    </html> 

原因是不執行的是你曾經函數稱爲myFunctions,但實際上你是爲onclick事件調用myFunction()

0

首先你要定義主爲false,那麼你就聲明瞭兩個功能:'myFunction'和'myFunctions',然後檢查master ==是否爲true。由於主人是虛假的,所以你不會看到警報。

當您單擊按鈕時,只有函數myFunction或myFunctions被執行。測試'master == true'不會再次執行,它不是函數的一部分