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>
Lưu đồ thuật toán: