HTML :
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>단락의 배경색을 설정하는 JavaScript 프로그램 작성</title>
</head>
<body>
<input type="button" value="Click to set paragraph background color" onclick="set_background()">
<p>365ok.co.kr JavaScript Exercises</p>
<p>365ok.co.kr PHP Exercises</p>
</body>
</html>
JS :
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)";
}
등록된 댓글이 없습니다.