JS-DOM속성(attribute) 제어

김진우·2023년 8월 10일
0

Javascript

목록 보기
40/48
  • 속성제어 : 속성은 attributue,
    ex)href, src, alt 등등

  • getAttribute : 속성의 값을 가져오기(보여주기)

  • setAttribute : 속성의 값을 변경하기

    • setAttribute("1","2"); -> 1이라는 속성의 값을 2로 변경 이라는 뜻

ex)예시

<a href = "https://www.naver.com" target = "_blank" title = "네이버로 이동"> 네이버 </a>
const a = document.querySelector("a");

const a_href = a.getAttribute("href");
console.log(a_href);
// 네이버 주소 출력 됨.
const a_title = a.getAttribute("title");
console.log(a_title);
// "네이버로 이동" 이  출력됨

a.setAttribute("href", "https://www.nate.com");
a.setAttribute("target", "_self");
// target이 _blank 에서  _self로 바뀜
a.setAttribute("title", "네이트로 이동");
// title이 네이버로 이동 -> 네이트로 이동 으로 바뀜

글자 변경하기

a.innerText = "네이트"
//a태그 안의 텍스트("네이버")를 네이트로 변경시킴 
profile
Code log

0개의 댓글

관련 채용 정보