javascripの表に対する操作(二)

17025 ワード


  
  
  
  
  1. <html> 
  2. <head> 
  3.     <title> </title> 
  4.     <style type="text/css"> 
  5.      
  6.     </style> 
  7. </head> 
  8. <body> 
  9.     <table border="1" align="center" id="table" width="600"> 
  10.         <tr> 
  11.             <th> </th> 
  12.             <th> </th> 
  13.             <th> </th> 
  14.             <th> </th> 
  15.         </tr> 
  16.         <tr> 
  17.             <td> </td> 
  18.             <td>25</td>      
  19.             <td> </td>   
  20.             <td>15339125250</td>     
  21.         </tr> 
  22.         <tr> 
  23.             <td> </td> 
  24.             <td>50</td>      
  25.             <td> </td>   
  26.             <td>15339125251</td>     
  27.         </tr>    
  28.         <tr> 
  29.             <td> </td> 
  30.             <td>20</td>      
  31.             <td> </td>   
  32.             <td>15339125252</td>     
  33.         </tr> 
  34.         <tr> 
  35.             <td> </td> 
  36.             <td>19</td>      
  37.             <td> </td>   
  38.             <td>15339125253</td>     
  39.         </tr> 
  40.         <tr> 
  41.             <td> </td> 
  42.             <td>25</td>      
  43.             <td> </td>   
  44.             <td>15339125254</td>     
  45.         </tr> 
  46.         <tr> 
  47.             <td> </td> 
  48.             <td>12</td>      
  49.             <td> </td>   
  50.             <td>15339125256</td>     
  51.         </tr> 
  52.     </table> 
  53.  
  54.     <script type="text/javascript"> 
  55.         //  
  56.         var tb = document.getElementById("table"); 
  57.         // ,  
  58.         for (var i = 1; i < tb.rows.length; i++) { 
  59.             var tr = tb.rows[i]; 
  60.             for (var j = 0; j < tr.cells.length; j++) { 
  61.                 var td = tr.cells[j]; 
  62.                 td.onclick = beginEdit
  63.             } 
  64.         } 
  65.  
  66.         function beginEdit(e){ 
  67.             //  
  68.             ee = e || window.event; 
  69.             // ( ) 
  70.             var target = e.target || e.srcElement; 
  71.             // If  
  72.             if (target.tagName == "TD") { 
  73.                 var inputs = target.getElementsByTagName("input"); 
  74.                 if (inputs == false || inputs.length == 0){  
  75.                     //  
  76.                     var text = target.innerHTML; 
  77.                     //  
  78.                     target.innerHTML = ""
  79.                     // input  
  80.                     var input = document.createElement("input"); 
  81.                     // input target  
  82.                     target.appendChild(input); 
  83.                     // input  
  84.                     input.type = "text"
  85.                     input.size = "5"
  86.                     input.focus(); 
  87.                     // input  
  88.                     input.value = text
  89.                      
  90.                     //input  
  91.                     input.onblur = function(e){ 
  92.                         ee = e || window.event; 
  93.                         // input 
  94.                         var target = e.target || e.srcElement; 
  95.                         var td = target.parentNode; 
  96.                         //innerHTML input , input 
  97.                         td.innerHTML = target.value; 
  98.                     } 
  99.                 } 
  100.             } 
  101.         } 
  102.  
  103.  
  104.     </script> 
  105. </body> 
  106. </html> 
 このセグメントコードは、セルの編集を実現します.
この記事は「聚沙成塔」のブログからのものです.転載はお断りします.