JavaScript/실습

실습 - innerText, innerHTML 프러퍼티

yunajoe 2023. 1. 28. 16:16

innerText 버튼을 클릭했을때

 

innerHTML 버튼을 클릭했을 때

 

 

나의풀이

 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>

  </style>
</head>
<body>
  <div id="btns">
    <button type="button" onclick="original()">innerText로 표시하기</button>
    <button type="button" onclick="italic()">innerHTML로 표시하기</button>
  </div>
  <br>

  <div id="time">
    <h1>현재시각:</h1>
    <br>      
  </div>
  <script>
    var now = new Date(); // 현재 날짜 및 시간 
    document.querySelector('#time').innerText += now;  

    function italic(){
      document.getElementById('time').style.fontStyle = "italic";
    }  
    function original(){
      document.getElementById('time').style.fontStyle = "normal";
    }   
  </script>
  
</body>
</html>

 

 

해설풀이

 

<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>innerText, innerHTML 프로퍼티</title>
</head>
<body>
	<button onclick="inntext()">innerText로 표시하기</button>
	<button onclick="innhtml()">innerHTML로 표시하기</button>
	<h1>현재 시각: </h1>
	<div id="current"></div>
	
	<script>
		var now = new Date();  
		function inntext(){
			document.getElementById("current").innerText = now;
		}
		function innhtml() {
			document.getElementById("current").innerHTML = "<em>" + now + "</em>";
		}
	</script>
</body>
</html>