attr( attributeName ) : 해당 attribute의 값을 가져오기
ex ) $("div").text($("img").attr("alt"));
➡️ div 태그의 img속성의 설명을 출력.
attr( attributeName, value ) : 해당 attribute에 값을 설정하기
ex ) $("img").attr("src", "../images/im0.jpg") .attr("title", "똘이군") .attr("alt", "puppy picture");
➡️ img 태그의 src 속성에 값을 "../images/im0.jpg"로,
title 속성에 값을 "똘이군"으로
alt 속성에 값을 "puppy picture"로 설정한다.
attr( attributeName, function() ) : 해당 엘리먼트의 속성 값을 콜백함수에서 얻어와서 설정하기
ex ) $("img").attr("src", function()
{ return "../images/" + this.title; });
➡️ 여기서 this는 $("img")와 같다.
ex ) $("img").attr("title", function(index)
{ return index +"번 엘리먼트로 타이틀은 " + this.title + "이다."; });
➡️ 함수에 index를 기술하면 0부터 시작되는 인덱스가 함수로 전달됨.
removeAttr(attributeName) : 해당 어트리뷰트의 값 제거하기
ex ) $("img").removeAttr("src");
alt는 이미지를 찾지 못했을 경우 이를 대체해 출력되는 텍스트를 말한다.
val() : <form> 태그에서 value속성값 가져오기
'Front > JS & jQuery' 카테고리의 다른 글
[jQuery] Event (0) | 2022.04.04 |
---|---|
[jQuery][Datatables]clear( ) / destroy() / empty() (0) | 2022.04.03 |
[jQuery] jQuery 입력 양식 필터 선택자 (0) | 2022.04.01 |
[jQuery] attribute를 조건으로 원하는 element 찾기 (0) | 2022.03.31 |
[jQuery] 래퍼(wrapper)와 셀렉터(Selector) (0) | 2022.03.30 |