개발/js·jquery
jquery | .not() 특정 선택자만 제외하기
HHHONG
2022. 10. 18. 20:53
반응형
.not()
jquery 선택자 중에 특정 요소(특정 클래스, 특정 아이디)만 제외하고 싶은 경우 아래와 같이 사용하면 된다.
.not(selector)
사용 예시
1) 클래스명이 link인 것만 제외한 a 태그
$('a').not('.link').css();
2) 링크 주소가 #인 것만 제외한 a태그
$('a').not('[href^=#]').css();
3) 클래스명이 content 가 아닌 #section
$('#section').not('.content').css();
.not() 여러개 지정하기
아래와 같이 두가지 방법으로 사용 가능하다.
[방법1] 선택자(여기서는 a태그) 밖에 .not()을 사용하여 '제외할 선택자1, 제외할선택자2' 로 표기한다.
※ 주의 ※ 작은 따옴표 안에 제외할 선택자를 콤마로 구분하여 넣어준다. 개별적으로 작은 따옴표 표기 X
$('a').not('.link, .link2').css();
이렇게 클래스명과 아이디 값을 같이 사용해도 된다.
$('a').not('.link, #link2').css();
특정 속성값을 가진 경우도 제외가 가능하다.
$('a').not('[href^=#],[href*=javascript]').css();
[방법2] 선택자 안에 :not(제외할선택자1, 제외할 선택자2) 로 표기한다.
$('a:not([href^=#], [href*=javascript])').css();
$('a:not(.link, .link2)).css();
참고)
.not() | jQuery API Documentation
Description: Remove elements from the set of matched elements. Given a jQuery object that represents a set of DOM elements, the .not() method constructs a new jQuery object from a subset of the matching elements. The supplied selector is tested against eac
api.jquery.com
반응형