제이쿼리 .attr (attributeName)
설명 : 일치하는 요소 세트의 첫 번째 요소에 대한 속성 값을 가져옵니다.
이 .attr()메서드는 일치하는 집합 의 첫 번째 요소에 대해서만 특성 값을 가져옵니다 . 각 요소의 값을 개별적으로 가져 오려면 jQuery .each()또는 .map()메서드 와 같은 루핑 구문을 사용하십시오 .
jQuery의 .attr()메서드를 사용 하여 요소의 속성 값을 얻는 데는 두 가지 주요 이점이 있습니다.
편리 성 : jQuery 객체에서 직접 호출 할 수 있고 다른 jQuery 메소드와 연결될 수 있습니다.
브라우저 간 일관성 : 일부 속성의 값은 브라우저간에 일관성없이보고되고 단일 브라우저의 여러 버전에서도 일치하지 않습니다. 이 .attr()방법은 이러한 불일치를 줄입니다.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>attr demo</title>
<style>
p {
margin: 20px 0 0;
}
b {
color: blue;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<input id="check1" type="checkbox" checked="checked">
<label for="check1">Check me</label>
<p></p>
<script>
$( "input" )
.change(function() {
var $input = $( this );
$( "p" ).html( ".attr( 'checked' ): <b>" + $input.attr( "checked" ) + "</b><br>" +
".prop( 'checked' ): <b>" + $input.prop( "checked" ) + "</b><br>" +
".is( ':checked' ): <b>" + $input.is( ":checked" ) + "</b>" );
})
.change();
</script>
</body>
</html>
'IT > 제이쿼리' 카테고리의 다른 글
제이쿼리 Attribute Equals Selector [name=”value”] (0) | 2017.08.08 |
---|---|
제이쿼리 attributeContainsPrefix selector (0) | 2017.08.06 |
제이쿼리 .appendTo( target ) (0) | 2017.08.06 |
제이쿼리 andSelf() (0) | 2017.08.03 |
제이쿼리 all selector (0) | 2017.08.03 |