반응형

문법

let el = document.getElementsByClassName("article_comment")[0];

 

"article_comment"라는 클래스 이름을 가진 element들을 다 가져온다.

(※주의, 배열형태로 가져올 것이기 때문에, 번지수를 기입해줘야한다 [0])

이 때, 해당 클래스 이름을 가진 엘리멘트가 오로지 하나라면, [0]을 써서, 하나만 가져오도록 하자

 

el.innerText = ???

 

예를 들어 <p>(A)</p> 가 있다면, (A) 부분에 ???를 채워넣는 명령문이다.

 

 

예제코드

<script>
	window.onload = function () {
		console.log("bigger than 1");
		let el = document.getElementsByClassName("article_comment")[0];
		if ( el.innerText.length > 95 )  {
			let string = el.innerText;
			string = string.substr(0, 95);
			el.innerText = string + "...";
			let tag = document.createElement('strong');
			tag.innerText = "더보기";
			el.appendChild(tag);


		}
		console.log(el.length + "el.length ");
	}

</script>

window.onload = function () {

}

 

-> 이부분은 창이 로드가 되면 실행되는 블록이다. 

 

반응형