이 <select>
인 양식에서 일부 요소를 가져와야했습니다. 요소에 new_cat_inputs
클래스를 추가하고 document.querySelectorAll()
까지 가져올 것이라고 생각했습니다.
나는 많은 것들을 시도했지만이 일한 적이, 내가 마지막으로 사용 :JS - 클래스 이름이있는 document.querySelectorAll 이해
document.getElementsByClassName("new_cat_inputs")
그것은 해결 된 내 문제를 의미하지만 이해하고 싶다.
// first attempt, as I'd have used document.querySelector() which works this way
document.querySelectorAll(".new_cat_inputs");
// Logs an error which says it can't read the property "querySelectorAll" of null (body)
document.body.querySelectorAll(".new_cat_inputs");
// doesn't get the elements neither as the 1st attempt. Neither with document.body
document.querySelectorAll("label.new_cat_inputs, input.new_cat_inputs, select.new_cat_inputs");
내가 종종하는 데 도움이
MDN documentation하지만이 시간을 읽어 여기에
은 전에 시도 것입니다.
전체 문서 기능
document.querySelectorAll()
에 의해 자신의
클래스 여러 DOM 요소를 선택
하는 올바른 방법은 무엇입니까? document.body
이후
이는 document.body는'null', 당신의 DOM이로드되지 않았습니다 어쩌면,이 이벤트 리스너 내부에 코드를 삽입하려고 않았다 the following link
건배, 참조 : '창을 .addEventListener ('DOMContentLoaded', function() { // 코드 }) ' ? –
@boehm_s 당신 말이 맞아요. 'window.onload'가 충분하지 않았습니다. – AymDev
하지만 여전히 ... document.getElementsByClassName()은 왜 작동하고'document.querySelectorAll()'이 아닌가요? @ boehm_s – AymDev