2014-05-24 31 views
-1

我有以下的html代碼.. 現在我想添加一個圖像(說字母圖像Q)到單擊的元素.. 我是新來的JavaScript ..請介紹一下我們應該怎麼做同樣的..簡單的方法添加圖像到onclick事件上的元素javascript

在此先感謝..

<html> 
<head> 
    <title> 
     8*8 Queen 
    </title> 

下面是CSS代碼..

<style> 
     td { 
      width:70px; 
      height:70px; 
      background-color:white; 
      border:1px solid; 
     } 
     .black { 
      background:black; 
     } 
     .white { 
      background: white; 
     } 
     .red { 
      background:red !important; 
     } 
    </style> 

下面是JAV ascript代碼..

 //var window.boxFlag=new Array(); 
     //var window.i=0; 
     var myEntry = new Array(); 



     function init() { 
      for (var i = 1; i <= 8; i++) { 

       myEntry[i] = new Array(); 
       for (var j = 1; j <= 8; j++) { 
        myEntry[i][j] = 0; 
       } 

      } 


     } 

     function removeQueen(domObj) { 


     } 
     function place(domObj, row, col) { 


      var placeQueen = true; 


      if (myEntry[row][col] == 1) { 

       //if already added, remove entry 
       myEntry[row][col] = 0; 
       domObj.classList.toggle("red"); 

      } else { 

       //New entry 


       //column validate 
       for (var i = 1; i <= 8; i++) { 

        if (myEntry[i][col] == 1) { 
         placeQueen = false; 
         break; 
        } 

       } 

       //row validate 
       for (var j = 1; j <= 8; j++) { 

        if(myEntry[row][j]==1){ 

         placeQueen=false; 
         break; 

        } 

       } 

       //Diagonal validate 

       for(var i=row-1,j=col-1;i>0&&j>0;i--,j--){ 

        if(myEntry[i][j]==1){ 

         placeQueen=false; 
         break; 

        } 

       } 



       for(var i=row+1,j=col+1;i<=8&&j<=8;i++,j++){ 

        //alert(i); 

        if(myEntry[i][j]==1){ 

         placeQueen=false; 
         break; 

        } 

       } 

       for(var i=row+1,j=col-1;i<=8&&j>0;i++,j--){ 


        if(myEntry[i][j]==1){ 

         placeQueen=false; 
         break; 
        } 

       } 

       for(var i=row-1,j=col+1;i>0&&j<=8;i--,j++){ 


        if(myEntry[i][j]==1){ 

         placeQueen=false; 
         break; 

        } 

       } 


       if (placeQueen) { 
        //on success 
        myEntry[row][col] = 1; 
        //set to true 
        domObj.classList.toggle("red"); 
        //To toggle red with white/black 
       } 
       else { 

        alert("Not a proper place"); 

       } 
      } 


     } 



    </script> 
</head> 

HTML代碼體

<body onload="init();"> 

    <table> 
     <tr id="row1"> 
      <td class="black" onclick="place(this, 1, 1)"> 

      </td> 
      <td class="white" onclick="place(this, 1, 2)"></td> 
      <td class="black" onclick="place(this, 1, 3)"></td> 
      <td class="white" onclick="place(this, 1, 4)"></td> 
      <td class="black" onclick="place(this, 1, 5)"></td> 
      <td class="white" onclick="place(this, 1, 6)"></td> 
      <td class="black" onclick="place(this, 1, 7)"></td> 
      <td class="white" onclick="place(this, 1, 8)"></td> 

     </tr> 

     <tr id="row2"> 

      <td class="white" onclick="place(this, 2, 1)"></td> 
      <td class="black" onclick="place(this, 2, 2)"></td> 
      <td class="white" onclick="place(this, 2, 3)"></td> 
      <td class="black" onclick="place(this, 2, 4)"></td> 
      <td class="white" onclick="place(this, 2, 5)"></td> 
      <td class="black" onclick="place(this, 2, 6)"></td> 
      <td class="white" onclick="place(this, 2, 7)"></td> 
      <td class="black" onclick="place(this, 2, 8)"></td> 

     </tr> 
     <tr id="row3"> 
      <td class="black" onclick="place(this, 3, 1)"></td> 
      <td class="white" onclick="place(this, 3, 2)"></td> 
      <td class="black" onclick="place(this, 3, 3)"></td> 
      <td class="white" onclick="place(this, 3, 4)"></td> 
      <td class="black" onclick="place(this, 3, 5)"></td> 
      <td class="white" onclick="place(this, 3, 6)"></td> 
      <td class="black" onclick="place(this, 3, 7)"></td> 
      <td class="white" onclick="place(this, 3, 8)"></td> 

     </tr> 
     <tr id="row4"> 

      <td class="white" onclick="place(this, 4, 1)"></td> 
      <td class="black" onclick="place(this, 4, 2)"></td> 
      <td class="white" onclick="place(this, 4, 3)"></td> 
      <td class="black" onclick="place(this, 4, 4)"></td> 
      <td class="white" onclick="place(this, 4, 5)"></td> 
      <td class="black" onclick="place(this, 4, 6)"></td> 
      <td class="white" onclick="place(this, 4, 7)"></td> 
      <td class="black" onclick="place(this, 4, 8)"></td> 

     </tr> 
     <tr id="row5"> 
      <td class="black" onclick="place(this, 5, 1)"></td> 
      <td class="white" onclick="place(this, 5, 2)"></td> 
      <td class="black" onclick="place(this, 5, 3)"></td> 
      <td class="white" onclick="place(this, 5, 4)"></td> 
      <td class="black" onclick="place(this, 5, 5)"></td> 
      <td class="white" onclick="place(this, 5, 6)"></td> 
      <td class="black" onclick="place(this, 5, 7)"></td> 
      <td class="white" onclick="place(this, 5, 8)"></td> 

     </tr> 
     <tr id="row6"> 

      <td class="white" onclick="place(this, 6, 1)"></td> 
      <td class="black" onclick="place(this, 6, 2)"></td> 
      <td class="white" onclick="place(this, 6, 3)"></td> 
      <td class="black" onclick="place(this, 6, 4)"></td> 
      <td class="white" onclick="place(this, 6, 5)"></td> 
      <td class="black" onclick="place(this, 6, 6)"></td> 
      <td class="white" onclick="place(this, 6, 7)"></td> 
      <td class="black" onclick="place(this, 6, 8)"></td> 

     </tr> 
     <tr id="row7"> 
      <td class="black" onclick="place(this, 7, 1)"></td> 
      <td class="white" onclick="place(this, 7, 2)"></td> 
      <td class="black" onclick="place(this, 7, 3)"></td> 
      <td class="white" onclick="place(this, 7, 4)"></td> 
      <td class="black" onclick="place(this, 7, 5)"></td> 
      <td class="white" onclick="place(this, 7, 6)"></td> 
      <td class="black" onclick="place(this, 7, 7)"></td> 
      <td class="white" onclick="place(this, 7, 8)"></td> 

     </tr> 
     <tr id="row8"> 

      <td class="white" onclick="place(this, 8, 1)"></td> 
      <td class="black" onclick="place(this, 8, 2)"></td> 
      <td class="white" onclick="place(this, 8, 3)"></td> 
      <td class="black" onclick="place(this, 8, 4)"></td> 
      <td class="white" onclick="place(this, 8, 5)"></td> 
      <td class="black" onclick="place(this, 8, 6)"></td> 
      <td class="white" onclick="place(this, 8, 7)"></td> 
      <td class="black" onclick="place(this, 8, 8)"></td> 

     </tr> 
    </table> 

</body> 

</html> 
+0

請大肆削減代碼到是給你的問題的部分(一個或多個)。 –

回答

1

使用domObj.innerHTML="Q";place function開始,你必須做在CSS一些變化太大。

我創建了一個小提琴here

相關問題