.hasClass( className )
.hasClass( className )
설명 : 일치하는 요소가 주어진 클래스에 지정되었는지 여부를 판별하십시오.
요소에는 둘 이상의 클래스가 할당되어있을 수 있습니다. HTML에서는 클래스 이름을 공백으로 구분하여 나타냅니다.
<div id="mydiv" class="foo bar"></div>
다른 클래스가있는 경우에도 클래스에 요소가 할당되면 이 .hasClass()메서드가 반환 true됩니다. 예를 들어 위의 HTML을 보면 다음과 같이 반환됩니다 true.
$( "#mydiv" ).hasClass( "foo" )
마찬가지로 :
$( "#mydiv" ).hasClass( "bar" )
이것이 반환하는 동안 false:
$( "#mydiv" ).hasClass( "quux" )
jQuery 1.12 / 2.2부터이 메소드는 SVG를 포함한 XML 문서를 지원한다.
예:
'selected'가 클래스로 포함 된 단락을 찾습니다.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>hasClass demo</title>
<style>
p {
margin: 8px;
font-size: 16px;
}
.selected {
color: red;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p>This paragraph is black and is the first paragraph.</p>
<p class="selected">This paragraph is red and is the second paragraph.</p>
<div id="result1">First paragraph has selected class: </div>
<div id="result2">Second paragraph has selected class: </div>
<div id="result3">At least one paragraph has selected class: </div>
<script>
$( "#result1" ).append( $( "p:first" ).hasClass( "selected" ).toString() );
$( "#result2" ).append( $( "p:last" ).hasClass( "selected" ).toString() );
$( "#result3" ).append( $( "p" ).hasClass( "selected" ).toString() ) ;
</script>
</body>
</html>
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5276 | 2년 전 | 1336 | |||
| 5275 | 2년 전 | 1405 | |||
| 5274 | 기타 | 2년 전 | 821 | ||
| 5273 | JavaScript |
swallow
|
2년 전 | 1053 | |
| 5272 | node.js |
swallow
|
2년 전 | 748 | |
| 5271 | JavaScript |
swallow
|
2년 전 | 1498 | |
| 5270 | PHP |
swallow
|
2년 전 | 848 | |
| 5269 | node.js |
swallow
|
2년 전 | 684 | |
| 5268 | node.js |
swallow
|
2년 전 | 1217 | |
| 5267 | PHP |
swallow
|
2년 전 | 1599 | |
| 5266 | MySQL |
swallow
|
2년 전 | 1095 | |
| 5265 | PHP |
swallow
|
2년 전 | 2035 | |
| 5264 | JavaScript |
swallow
|
2년 전 | 861 | |
| 5263 | 기타 |
swallow
|
2년 전 | 2984 | |
| 5262 | 기타 |
swallow
|
2년 전 | 670 | |
| 5261 | node.js |
swallow
|
2년 전 | 1065 | |
| 5260 | JavaScript |
swallow
|
2년 전 | 1514 | |
| 5259 | node.js |
swallow
|
2년 전 | 736 | |
| 5258 | node.js |
swallow
|
2년 전 | 1749 | |
| 5257 | node.js |
swallow
|
2년 전 | 1301 | |
| 5256 | PHP |
swallow
|
2년 전 | 1941 | |
| 5255 | node.js |
swallow
|
2년 전 | 692 | |
| 5254 | PHP |
swallow
|
2년 전 | 3015 | |
| 5253 | node.js |
swallow
|
2년 전 | 680 | |
| 5252 | 정규표현식 |
swallow
|
2년 전 | 625 | |
| 5251 | node.js |
swallow
|
2년 전 | 882 | |
| 5250 | PHP |
swallow
|
2년 전 | 1116 | |
| 5249 | node.js |
swallow
|
2년 전 | 1066 | |
| 5248 | PHP |
swallow
|
2년 전 | 1411 | |
| 5247 | node.js |
swallow
|
2년 전 | 1019 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기