programing

jQuery: 특정 ID를 제외한 지정된 클래스의 모든 요소 선택

elecom 2023. 8. 6. 09:51
반응형

jQuery: 특정 ID를 제외한 지정된 클래스의 모든 요소 선택

이것은 아마도 꽤 간단할 것입니다.

주어진 클래스의 모든 요소를 선택합니다.thisClass신분증이 있는 곳을 제외하고는thisId.

즉, (여기서 -/delay는 제거를 의미함)와 동등한 것입니다.

$(".thisClass"-"#thisId").doAction();

:아닙니다 선택기를 사용합니다.

$(".thisclass:not(#thisid)").doAction();

ID 또는 선택기가 여러 개인 경우 쉼표 구분 기호를 사용하면 됩니다.

(".thisclass:not(#thisid,#thatid)").doAction();

또는 .not() 메서드를 사용합니다.

https://api.jquery.com/not/

$(".thisClass").not("#thisId").doAction();

누군가 찾고 있을 경우를 대비해 JS(ES6) 답변을 제출하겠습니다.

Array.from(document.querySelectorAll(".myClass:not(#myId)")).forEach((el,i) => {
    doSomething(el);
}

업데이트(원래 답변을 게시했을 때 가능했지만 지금 추가):

document.querySelectorAll(".myClass:not(#myId)").forEach((el,i) => {
    doSomething(el);
});

이것은 그것을 제거합니다.Array.from사용.

document.querySelectorAll를 반환합니다.NodeList.
여기를 읽고 반복하는 방법(및 기타 사항)에 대해 자세히 알아보십시오. https://developer.mozilla.org/en-US/docs/Web/API/NodeList

다음 예제와 같이 .not 함수를 사용하여 정확한 id, 특정 단어를 포함하는 id, 단어로 시작하는 id 등을 가진 항목을 제거할 수 있습니다.jQuery 실렉터에 대한 자세한 내용은 http://www.w3schools.com/jquery/jquery_ref_selectors.asp 을 참조하십시오.

정확한 ID로 무시:

 $(".thisClass").not('[id="thisId"]').doAction();

"Id" 단어가 포함된 ID 무시

$(".thisClass").not('[id*="Id"]').doAction();

"내"로 시작하는 ID 무시

$(".thisClass").not('[id^="my"]').doAction();

$(".thisClass[id!='thisId']").doAction();

실렉터에 대한 설명서: http://api.jquery.com/category/selectors/

사용.not()전체 요소를 선택하는 방법도 옵션입니다.

이 방법은 해당 요소로 직접 다른 작업을 수행하려는 경우 유용할 수 있습니다.

$(".thisClass").not($("#thisId")[0].doAnotherAction()).doAction();

언급URL : https://stackoverflow.com/questions/2551217/jquery-select-all-elements-of-a-given-class-except-for-a-particular-id

반응형