.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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4766 | jQuery | 8년 전 | 1673 | ||
| 4765 | jQuery | 8년 전 | 1349 | ||
| 4764 | jQuery | 8년 전 | 2163 | ||
| 4763 | jQuery | 8년 전 | 1853 | ||
| 4762 | jQuery | 8년 전 | 1625 | ||
| 4761 | jQuery | 8년 전 | 1932 | ||
| 4760 | jQuery | 8년 전 | 1296 | ||
| 4759 | jQuery | 8년 전 | 1596 | ||
| 4758 | jQuery | 8년 전 | 1310 | ||
| 4757 | jQuery | 8년 전 | 1273 | ||
| 4756 | jQuery | 8년 전 | 1496 | ||
| 4755 | jQuery | 8년 전 | 1646 | ||
| 4754 | jQuery | 8년 전 | 1612 | ||
| 4753 | jQuery | 8년 전 | 1766 | ||
| 4752 | jQuery | 8년 전 | 1519 | ||
| 4751 | jQuery | 8년 전 | 1838 | ||
| 4750 | jQuery | 8년 전 | 1981 | ||
| 4749 | jQuery | 8년 전 | 1440 | ||
| 4748 | PHP | 8년 전 | 2809 | ||
| 4747 | jQuery | 8년 전 | 1390 | ||
| 4746 | jQuery | 8년 전 | 1645 | ||
| 4745 | jQuery | 8년 전 | 1652 | ||
| 4744 | jQuery | 8년 전 | 1532 | ||
| 4743 | jQuery | 8년 전 | 1612 | ||
| 4742 | jQuery | 8년 전 | 1703 | ||
| 4741 | jQuery | 8년 전 | 1597 | ||
| 4740 | jQuery | 8년 전 | 1603 | ||
| 4739 | jQuery | 8년 전 | 1797 | ||
| 4738 | jQuery | 8년 전 | 1306 | ||
| 4737 | jQuery | 8년 전 | 2097 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기