const h1Elem1 = document.querySelector('h1'); // JavaScript로 태그 선택
const h1Elem2 = $('h1'); // jQuery로 태그 선택
h1Elem1 === h1Elem2
jQuery에서는 $('selecto')
로 선택자를 지정할 수 있다.
$('selector').attr('태그의 속성 key명', '세팅할 value')
attr()를 이용해서 값 가져오기
$('div1').attr('style')
attr()를 이용해서 값 변경하기
$('div1').attr('style', 'color : red')
jQuery의 .val()
는 JavaScript의 .value
와 역할이 비슷하다. 주로 select
와 input
의 값을 얻기 위해 사용된다.
$('selector').val('변경할 값');
let txt1 = document.getElementById('txt1').value; // JavaScript로 값 가져오기
let txt2 = $('#txt1').val(); // jQuery로 값 가져오기
txt1.value = 'text2'; // JavaScript로 값 변경하기
$('#txt1').val('text2'); // jQuery로 값을 가져옴과 동시에 변경하기
해당 포스팅은 인프런 - 퍼블리셔, 신입 프론트 개발자를 위한 실무에서 써먹는 실전 jQuery 설명서의 내용을 보며 공부한 것을 정리한 내용입니다.