본문 바로가기

Programming/Javascript

태그로 둘러싼 내용 innerHTML로 변경하기

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<h1 id="hello">만나서 반갑습니다.</h1>
	<h1 id="hello">만나서 반갑습니다.</h1>
	<button id="change">인사변경</button>
	<script type="text/javascript">
		var hello = document.getElementById('hello');
		var change = document.getElementById('change');
		change.addEventListener('click', function() {
			hello.innerHTML = '안녕히가세요';
		});
	</script>
</body>
</html>

 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<style>
div {
	width: 100%;
	margin: 0 auto;
	text-align: center;
	font-size: 40px;
}
</style>
</head>
<body>
	<div>
		<p id="view">0</p>
		<button id="up">+1 증가</button>
		<button id="down">-1 감소</button>
	</div>
	<script type="text/javascript">
		var view = document.getElementById('view');
		var up = document.getElementById('up');
		var down = document.getElementById('down');

		up.addEventListener('click', function() {
			view.innerHTML++;
		})
		down.addEventListener('click', function() {
			if (view.innerHTML > 0) {
				view.innerHTML--;
			} else {
				view.innerHTML;
			}
		})
	</script>
</body>
</html>