2012-05-25 84 views
-1

我已經完成了所有的編碼,一切工作都很好。只停留在JavaScript。我可以改變圖像背景的顏色。但是,當我點擊圖片時,我無法提示彈出消息,我嘗試使用「onclick」。但它仍然不起作用。JavaScript提示信息框不工作

任何人都可以給我建議或作品的例子。請看下面我的編碼 -

<html> 
<head> 
<style> 
      th{ 
       width:60px; 
       height:60px; 
      } 
      table{ 
       border: 5px solid #FFBB78; 
       border-collapse:collapse; 
      } 
      td{ 
       width:60px; 
       height:60px; 
      } 
      tr{ 
       width:60px; 
       height:60px; 
      } 
      h1{ 
       color:#6633FF; 
      } 
</style> 

<script type="text/javascript"> 
function changeColor(){ 
    document.getElementById("king").style.backgroundColor="black"; 
} 
function click(){ 
    /*var pawn = document.write.getElementById('pawn'); 
    var knight = document.write.getElementById('knight'); 
    var queen = document.write.getElementById('queen'); 
    var bishop = document.write.getElementById('bishop'); 
    var king = document.write.getElementById('king_1'); 
    var square = document.write.getElementById('sqaure');*/ 
    alert("This is a knight"); 
} 

/*function promt_chess_piece(){ 
    //var knight = document.write.getElementById('knight'); 
    if (knight.value.lenght) 
    { 
     alert("This is the knight"); 
     knight.focus(); 
     return true; 
    } 
}*/ 
</script> 
</head> 
<body> 
<?php 

    $pictures = array( 
     //row 1 
     "1,1" => '<img src="chess/br.gif" />', 
     "1,3" => '<img src="chess/bb.gif"/>', 
     "1,4" => '<img src="chess/bq.gif"/>', 
     "1,5" => '<img src="chess/bk.gif"/>', 
     "1,8" => '<img src="chess/br.gif"/>', 
     //row 2 
     "2,1" => '<img src="chess/bp.gif"/>', 
     "2,2" => '<img src="chess/bp.gif"/>', 
     "2,3" => '<img src="chess/bp.gif"/>', 
     "2,4" => '<img src="chess/bp.gif"/>', 
     "2,5" => '<img src="chess/bb.gif"/>', 
     "2,6" => '<img src="chess/bp.gif"/>', 
     "2,7" => '<img src="chess/bp.gif"/>', 
     "2,8" => '<img src="chess/bp.gif"/>', 

     //row 3 
     "3,3" => '<img src="chess/bn.gif" onclick="click()"/>', 
     "3,6" => '<img src="chess/bn.gif"/>', 

     //row 4 
     "4,5" => '<img src="chess/bp.gif"/>', 

     //row 5 
     "5,3" => '<img src="chess/wb.gif"/>', 
     "5,5" => '<img src="chess/wp.gif"/>', 

     //row 6 
     "6,4" => '<img src="chess/wp.gif"/>', 
     "6,6" => '<img src="chess/wn.gif"/>', 

     //row 7 
     "7,1" => '<img src="chess/wp.gif"/>', 
     "7,2" => '<img src="chess/wp.gif"/>', 
     "7,3" => '<img src="chess/wp.gif"/>', 
     "7,6" => '<img src="chess/wp.gif"/>', 
     "7,7" => '<img src="chess/wp.gif"/>', 
     "7,8" => '<img src="chess/wp.gif"/>', 

     //row 8 
     "8,1" => '<img src="chess/wr.gif"/>', 
     "8,2" => '<img src="chess/wn.gif"/>', 
     "8,3" => '<img src="chess/wb.gif"/>', 
     "8,4" => '<img src="chess/wq.gif"/>', 
     "8,6" => '<img src="chess/wr.gif"/>', 
     "8,7" => '<img src="chess/wk.gif" onmouseover="changeColor()" id="king" />',);//array ends here 

    echo"<h1 align='center'>SAJID Chess Board</h1>"; 
    echo"<table border='1' align='center'>"; 

    for($i = 1; $i <= 8; $i++){  
      echo "<tr>"; 
      for($j = 1; $j <=8; $j++){ 
       if(($i+$j)%2==0) { 
       echo"<td bgcolor='#99FF99'>"; 
       } 
       else { 
       echo"<td bgcolor='#9999CC'>"; 
        } 


       if(isset($pictures["{$i},{$j}"]))//compares the pictures i and p 
        echo $pictures["{$i},{$j}"]; 

       echo "</td>"; 
       } 
       echo "</tr>"; 
      } 


      echo "</table>"; 

     ?> 
    </body> 
</html> 
+2

你能告訴我們代碼,以便我們可以幫忙嗎? –

+0

看着你的代碼,我沒有看到任何立即出錯的地方(這裏有很多代碼)。查看JavaScript控制檯,看看您的瀏覽器不喜歡該頁面,如果有的話。另外,看看這個PHP產生的實際頁面源碼。您是否看到您的img標籤,並且按照您所期望的方式點擊寫入頁面?正如書面所說,這個電話沒有任何明顯的錯誤。 –

回答

0

您確定點擊圖片的onclick屬性?只有一個圖像有這個。這是我能看到的唯一可能成爲問題的事情。