728x90
.attr()은 요소(element)의 속성(attribute)의 값을 가져오거나 속성을 추가합니다.
문법 1
.attr( attributeName )
선택한 요소의 속성의 값을 가져옵니다. 예를 들어
$( 'div' ).attr( 'class' );
는 div 요소의 class 속성의 값을 가져옵니다.
<p>p tag</p>
<button type="button">버튼</button>
<input type="button" id = "btn" value = "input">
<script type="text/javascript">
$(document).ready(function() {
//setter
$("button").click(function() {
$("p").css("background","blue");
$('p').text("여기는 p 태그");
//getter
let color = $("p").css("background");
/* alert(color); */
//attribute
//속성을 추가
$('p').attr("id","pid");//p 태그에 아이디를 추가
//alert($('p').attr("id"));
//$('p').attr("class","back");
$('p').addClass("back");
//html(), text(), val(), css(), attr(), prop()
});
//클래스를 지워준다.
$("#btn").click(function() {
$("p").removeClass("back");
})
.back{
color: #ffff00;
}
728x90
'HTML' 카테고리의 다른 글
38일차//JQuery// 과제 (요소값 변경, 값 가져오기) (0) | 2021.03.08 |
---|---|
jquery 자주 쓰는 함수 최종정리( .addClass / .css / .text / .html / .append / .prepend (0) | 2021.03.08 |
37일차//JQuery// .append( ) .prepend( ) (0) | 2021.03.05 |
37일차//JQuery// .click() (0) | 2021.03.05 |
37일차//JQuery// text() (0) | 2021.03.05 |