2016-06-22 1 views
0

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 솔루션이 필요 없다.

감사합니다.

+1

당신이 그들 모두를 반복해야합니다 (아래에 두 가지 큰 해답이 있지만, 당신을 위해 자바 스크립트 라이브러리를 사용하고 싶지 않다면, ** 회 돌이 (for ** loop)를 사용할 수도 있습니다 (getElementsByClassName은 배열을 반환합니다). – briosheje

+0

'document.getElementsByClassName ('Bound')'를 반복하고 목록의 각 요소에 대해 onfocus 이벤트를 적용해야합니다. –

+0

모두가 그렇게 빨리 뛰어 오르는 것에 감사드립니다. 모두에게 감사드립니다! – david

답변

6

모든 요소를 ​​반복하려면 루프를 사용하십시오.

사용 Array#forEach, forEach() 방법은 배열 요소마다 한번 제공된 기능을 실행한다.

또 다른 대안은 직접 반환 된 결과를 통해 Array# 메소드를 호출 할 수 있도록 getElementsByClassName에 의해 반환 HTMLCollection 이상 Array.from를 사용하는 수.

var inputBound = document.getElementsByClassName('Bound'); 
 
[].forEach.call(inputBound, function(inputBound) { 
 
    inputBound.onfocus = function() { 
 
    var input = this.value; 
 
    formFunct(input); 
 
    } 
 
}) 
 

 
function formFunct(input) { 
 
    console.log('done did the form Funct: ' + input) 
 
}
<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" />

주 :

+1

그것은'.call '을 영리하게 사용합니다. 아마도 그것은 OP를 위해 상당히 진보 된 것입니다 (그렇지 않다면 그가 요구하지 않았을 것입니다). 그러나 그것은 확실히 최고의 해결책입니다. 훌륭한 접근입니다. – briosheje

+0

또는 일반 for 루프 만 사용하면 모든 브라우저에서 더 빠르고 지원할 수 있습니다. –

+0

@briosheje, 그것에 대해 메모를 추가했습니다 .. 감사;) – Rayon

4

반복 처리를 사용하여 요소에 이벤트 핸들러를 첨부 할 수 있습니다.

// get all elements and convert to array 
 
Array.from(document.getElementsByClassName('Bound')) 
 
    // iterate overa array elements 
 
    .forEach(function(ele) { 
 
    // bind event handler 
 
    ele.onfocus = function() { 
 
     var input = this.value; 
 
     formFunct(input); 
 
    } 
 
    }); 
 

 
function formFunct(input) { 
 
    console.log('done did the form Funct: ' + input) 
 
}
<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" />

+1

나는 이것을 정말로 좋아한다. 간단하고 이해하기 쉽습니다. 고맙습니다. – david

3

단순히 반복 모든 (과 좋은 오래된 : 루프 용) NodeList에서 Node (들)

inputBounds = document.getElementsByClassName('Bound'); 
for(var counter = 0; counter < inputBounds.length; inputBounds++) 
{ 
    inputBounds.item(counter).onfocus = function() { 
     var input = this.value; 
     formFunct(input); 
    } 
}