Javascript - Cập nhật nội dung của một ô trong bảng

Viết một hàm JavaScript chấp nhận hàng, cột, và một chuỗi để cập nhật nội dung của ô đó.

Mã nguồn:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=utf-8 /> 
<title>Change the content of a cell</title> 
<style type="text/css"> 
body {margin: 30px;} 
</style>  
	<script>
	function changeContent()
		{
		rn = window.prompt("Input the Row number(0,1,2)", "0");
		cn = window.prompt("Input the Column number(0,1)","0");
		content = window.prompt("Input the Cell content");  
		var x=document.getElementById('myTable').rows[parseInt(rn,10)].cells;
		x[parseInt(cn,10)].innerHTML=content;
		}
	</script>
</head><body> 
<table id="myTable" border="1"> 
<tr><td>Row1 cell1</td> 
<td>Row1 cell2</td></tr> 
<tr><td>Row2 cell1</td> 
<td>Row2 cell2</td></tr> 
<tr><td>Row3 cell1</td> 
<td>Row3 cell2</td></tr> 
</table><form> 
<input type="button" onclick="changeContent()" value="Change content"> 
</form></body></html>

Xem ví dụ

Lưu đồ thuật toán:

Flowchart: JavaScript - Create a function to update the content of a specific cell of a table.



Chatbot Tư vấn Lộ trình CNTT 🤖
Chào bạn! Tôi có thể tư vấn về các lộ trình học CNTT dựa trên roadmap.w3typing.com. Hãy chọn một từ khóa dưới đây hoặc gõ câu hỏi của bạn (ví dụ: "Frontend", "Backend", "Python", "DevOps").