<body>
<h1 id="hd"> this is heading </h1>
<!-- <input button = "button" onclick="fn()">스타일 변경 / 내용변경</button> -->
<input type = "button" id="btn" onclick="fn()" value= "스타일 변경 / 내용변경">
<script>
function fn() {
var v_headTag = document.getElementById('hd');
//console.log(v_headTag.innerHTML);
v_headTag.innerHTML = "내용변경...";
v_headTag.style.color = "red";
var v_btn = document.getElementById('btn');
//console.log(v_btn.value);
v_btn.value = "button change";
}
</script>
</body>
= 보안점검을 거치는 프로젝트의 경우 코드 거부 가능성이 있으므로 특별한 경우를 제외하고 사용을 권장하지 않음
<script>
// 공통으로 사용하기 위해 전역변수로
var v_d1 = document.getElementById('d1');
function fn1(){
alert(v_d1.innerHTML);
}
function fn2(){
alert(v_d1.innerText);
}
function fn3(){
alert(v_d1.textContent);
}
fn1()
fn2()
fn3()
이런 예제...
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel = "stylesheet" href ="../css/outStyle.css">
</head>
<style>
/*span {
display : none;
}*/
</style>
<body>
<div id = "d1">
안녕? 만나서 반가워
<!--인라인으로 하지말래<span style ="display:none;"> 숨겨진 텍스트... </span> -->
<span style ="display:none;"> 숨겨진 텍스트...2 </span>
</div>
<input type ="button" value="innerHTML" onclick="fn1()">
<input type ="button" value="innerTEXT" onclick="fn2()">
<input type ="button" value="textContent" onclick="fn3()">
<input type ="button" value="value" onclick="fn4()">
<div id="result">
</div>
<script>
// 공통으로 사용하기 위해 전역변수로
var v_d1 = document.getElementById('d1');
var v_rst = document.getElementById('result');
function fn1(){
alert(v_d1.innerHTML);
v_rst.innerHTML ="<p style='color:green'> 나는 innerHTML입니다 </p>";
}
function fn2(){
alert(v_d1.innerText);
v_rst.innerText ="<p style='color:green'> 나는 innerText입니다 </p>";
}
function fn3(){
alert(v_d1.textContent);
v_rst.textContent ="<p style='color:green'> 나는 textContent입니다 </p>";
}
function fn4(){
var v_btn = document.getElementsByTagName('input')[3];
alert(v_btn.value);
v_btn.value="value_change";
}
</script>
</body>
</html>
ES6에서 나온 새로운 방법! (ES2에서 나온 getElementId()가 더 효율적)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel = "stylesheet" href ="../css/outStyle.css">
<style>
div {
border : 1px solid black;
width : 200px;
height : auto;
margin : 10px;
padding: 10px;
}
p{
border : 1px solid gray;
}
</style>
</head>
<body>
<script>
function getCN() {
// 1) 클래스네임이 p인것의 길이 띄우기
var v_pTag= document.getElementsByClassName('p');
alert(v_pTag.length);
// 2) 클래스네임이 p인것중 인덱스가 3번인것의 색을 빨갛게 바꾼다
// JS는 이런거 할때 딱 하나의 요소만 지정해줘야 돌아감
var v_pTag= document.getElementsByClassName('p')[3];
v_pTag.style.color ="red";
// 즉 여러개의개체에 대해 스타일을 적용 할 때 for문을 사용한다
// 3) 클래스네임이 p인것들의 border스타일을 바꾼다
var v_pArr= document.getElementsByClassName('p');
for(var i=0; i<v_pArr.length; i++){
v_pArr[i].style.border = "1px solid red";
}
// 4) 쿼리셀렉터All도 for문 사용
var v_pArr = document.querySelectorAll('.p');
for(var i=0; i<v_pArr.length; i++){
v_pArr[i].style.color ="orange";
}
// 5) .p의 내용물을 오렌지색으로 바꾼다
// 쿼리셀렉터는 가장 첫번째 항목만을 선택
var v_pTag = document.querySelector('.p');
v_pTag.style.color ="pink";
}
function getTN(){
//getElementsByTagName을 이용해 div스타일 적용하기
var v_pTN = document.getElementsByTagName('div');
for(var i=0; i<v_pTN.length; i++){
v_pTN[i].style.border = "5px dotted blue";
}
}
</script>
<div class="d"> 첫번째 div1
<p class ="p">첫번째 p1</p>
<p class ="p">p2</p>
<p class ="p">p3</p>
<p class ="p">p4</p>
</div>
<div class="d">div2</div>
<div class="d">div3</div>
<div class="d">div4</div>
<input type = "button" value="classNm" onClick="getCN()">
<input type = "button" value="tagNm" onClick="getTN()">
</body>
</html>