Javascript - Đặt màu nền

Viết chương trình JavaScript để đặt màu nền của đoạn văn.

Mã nguồn:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Set the background color of a paragraph</title>
	<script>
	function set_background() {
	 docBody = document.getElementsByTagName("body")[0];
	  //Get all the p elements that are descendants of the body
	  myBodyElements = docBody.getElementsByTagName("p");
	// get the first p elements
	  myp1 = myBodyElements[0];
	  myp1.style.background = "rgb(255,0,0)";
	// get the second p elements
	  myp2 = myBodyElements[1];
	  myp2.style.background = "rgb(255,255,0)";
	}
	</script>
</head>
<body>
<input type="button" value="Click to set paragraph background color" onclick="set_background()">
<p> JavaScript Exercises</p>
<p> PHP Exercises</p> 
</body>
</html>

Xem ví dụ

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

Flowchart: JavaScript - Set the background color of a paragraph.


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").