2015-11-06 71 views
1

我是一名JavaScript初學者。我正在嘗試編寫一個表格,並在點擊單元格時更改其內容。這就是我試圖做的:點擊更改表格數據 - javascript

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <style> 
 
    table, 
 
    td { 
 
     border: 3px solid black; 
 
    } 
 
    </style> 
 
</head> 
 

 
<body> 
 

 
    <table id="myTable"> 
 
    <tr> 
 
     <td onclick="this.value ='Change 1';">cell 1</td> 
 
     <td onclick="this.value ='Change 2';">cell 2</td> 
 
    </tr> 
 

 
    </table> 
 

 
</body> 
 

 
</html>

回答

2

td沒有value屬性,則需要使用innerHTMLtextContent代替

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <style> 
 
    table, 
 
    td { 
 
     border: 3px solid black; 
 
    } 
 
    </style> 
 
</head> 
 

 
<body> 
 

 
    <table id="myTable"> 
 
    <tr> 
 
     <td onclick="this.innerHTML ='Change 1';">cell 1</td> 
 
     <td onclick="this.innerHTML ='Change 2';">cell 2</td> 
 
    </tr> 
 
    </table> 
 

 
</body> 
 

 
</html>