2012-05-25 60 views
0

我在php中創建了一個棋盤,並根據它們的順序放置了棋子。一切正常,但我無法改變國王的顏色,當我點擊鼠標時,我必須使用JavaScript來提示消息給用戶關於國際象棋的細節,當用戶點擊任何一塊國際象棋,在這裏我必須使用javaScript來做到這一點。有沒有什麼建議,我應該怎麼辦。請參考下面的代碼,我應該更改或添加什麼才能使其工作。JavaScript和php,與陣列圖像發生故障

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

<script type="text/javascript"> 
function changeColor(){ 
    document.getElementById("king").style.backgroundColor="yellow";//this image has to change color. 
} 
</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"/>', 
     "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"/>',//this are the chess piece that has to prompt out chess piece details 
     "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"/";);//this image has to change color.//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> 
+0

onmouseover不是標籤。 – Cfreak

回答

3

onmouseover不是標籤,它是標籤的屬性。 onmouseover需要成爲img標籤的一部分。改變這條線會讓你走上正軌,如下所示:

"8,7" => '<img src="chess/wk.gif" onmouseover="changeColor()" id="king" />',);//array ends here 
+0

ooooo,yeaahhhh! Thansks超現實..只爲1行我坐了一個多小時..謝謝你很多 – bleach64

+0

也許你還需要包括jquery和移動onmouseover到jquery事件,基於這個問題;那麼其他作品甚至空白的廣場呢?只需給所有作品/廣場上課,然後從那裏改變。 – 2012-05-25 14:55:22

+0

o,感謝您的建議。 :-) – bleach64