div内のすべての子タグを取得したかった:
<div class="form input">
<label class="formLabel cFont cColor cMore" id="text" for="textInput">please input something</label>
<input class="formInput" type="text" id="textInput">
<label class="formLabel" id="check" for="checkboxInput">check here to toggle disable</label>
<input class="formInput" type="checkbox" id="checkboxInput">
</div>
<button type="button" onclick="showClasses()">show classes</button>
私は次のことをしました:
function showClasses(){
var children = $('.form').children();
children.each(function(index, v){
var classes = v.attr('class');
console.log(classes);
}); // end each
} // end showClasses
コンソールにエラーが表示されました:
Uncaught TypeError: Object #<HTMLLabelElement> has no method 'attr'
次に、コードを次のように変更します。
function showClasses(){
var children = $('.form').children();
children.each(function(){
var classes = $(this).attr('class');
console.log(classes);
}); // end each
} // end showClasses
それは動作します。
.each(function(index, element)) と .each(function()) の違いは何ですか? コードで最初のものを使用できないのはなぜですか?