2016-06-22 8 views
0

classNameでバインドされたスクリプトを作成しようとしていて、別の関数を呼び出しています。javascript:classnameで関数を呼び出す

私はこのコードを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

あなたは(それらのすべてを反復処理する必要があります下に2つの大きな答えがありますが、あなたはそれを行うjavascriptライブラリを使用しない限り、通常の** for **ループを使用することもできます(getElementsByClassNameは配列を返します)。 – briosheje

+0

'document.getElementsByClassName( 'Bound')'をループし、リストの各要素に 'onfocus'イベントを適用する必要があります。 –

+0

すごくすばやく飛び込んでくれた皆さん、本当にありがとうございます。みなさんありがとう! – david

答えて

6

すべての要素を繰り返し処理するには、ループを使用します。

使用Array#forEachforEach()方法は、一度配列要素ごとに設けられた機能を実行します。

別の代替を使用すると、返される結果の上に直接Array#メソッドを呼び出すことができるようにgetElementsByClassNameによって返さHTMLCollectionArray.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

私はこの1つを本当に好きです。シンプルで分かりやすいありがとうございました。 – david

3

単に(:) forループ古き良きで)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); 
    } 
} 
関連する問題