JavaScript

[jQuery] selector 와일드 카드

나는아이스크림 2022. 10. 13. 14:01
728x90
<th id="option_0">hi</th>
<th id="option_1">hi</th>
<th id="option_2">hi</th>
<th id="option_3">hi</th>

위 코드와 같이 option_ 까지 공통일 경우 jQuery로 한꺼번에 선택해서 작업하고 싶을 때가 있다.
이럴 때 와일드카드를 사용하면 된다.

 

  • 시작 문자열로 찾기
$("[id^='option']").css( ... );

// for 문으로 하나씩 선택
var options = document.querySelectorAll("[id^='option']");
for(var i = 0; i < options.length; i++) {
	// options[i] 으로 사용 가능
}

 

  • 끝 문자열로 찾기
$("[id$='option']").css( ... );

 

  • 중간 문자열로 찾기
$("[id*='option']").css( ... );

 

 

728x90
728x90