.querySelector() 사용시 [, 대괄호 , square bracket 이스케이프 escape

document.getElementById() 라면 이스케이프 처리 필요 없음
document.querySelector()의 경우 []가 애트리뷰트 값을 감싸는데 사용되므로 해당 애트리뷰트의 값에 사용된다면
\를 두개 붙여서 이스케이프 처리 해줘야한다.

이 때까지 그렇게 selector 한 적이 없어서... 와 오늘 새로 알았음.




document.getElementById('a[a]')  <-- 이건 동작함
<span id="a[a]">22-01-12 12:11</span>
document.querySelector('#a[a]')
null
document.querySelector('#a\[a\]')
null
 
document.querySelector('#a\\[a\\]')  <-- \ 두개로 이스케이프 처리해주면 동작함
<span id="a[a]">22-01-12 12:11</span>
댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -
목록형 📅 달력형