className에 바인딩 된 스크립트를 작성하려고 시도하고 다른 함수를 호출합니다.javascript : 클래스 이름별로 함수 호출
이 코드는 className의 첫 번째 인스턴스에서 작동하지만 스크립트의 첫 번째 줄에서 [0]
을 제거하면 더 이상 작동하지 않습니다.
<input type="text" value="NOTBound" class="NOTBound"/>
<input type="text" value="Bound value 1" class="Bound"/>
<input type="text" value="NOTBound" class="NOTBound"/>
<input type="text" value="Bound value 2" class="Bound"/>
<script>
inputBound = document.getElementsByClassName('Bound')[0];
inputBound.onfocus = function() {
var input = this.value;
formFunct(input);
}
function formFunct(input) {
console.log('done did the form Funct: ' + input)
}
</script>
className="Bound"
으로 모든 입력에 대해 어떻게 작동합니까? 나는 jQuery 솔루션이 필요 없다.
감사합니다.
당신이 그들 모두를 반복해야합니다 (아래에 두 가지 큰 해답이 있지만, 당신을 위해 자바 스크립트 라이브러리를 사용하고 싶지 않다면, ** 회 돌이 (for ** loop)를 사용할 수도 있습니다 (getElementsByClassName은 배열을 반환합니다). – briosheje
'document.getElementsByClassName ('Bound')'를 반복하고 목록의 각 요소에 대해 onfocus 이벤트를 적용해야합니다. –
모두가 그렇게 빨리 뛰어 오르는 것에 감사드립니다. 모두에게 감사드립니다! – david