「jQuery入門」.each()メソッドの使い方

書式
$('セレクタ’).each(function(index, element){
//some code
});

1.引数を省略

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var count = 0;
$(".cft").each(function() {
console.log(count + ': ' + $(this).text());
count++;
});
var count = 0; $(".cft").each(function() { console.log(count + ': ' + $(this).text()); count++; });
var count = 0;
$(".cft").each(function() {
    console.log(count + ': ' + $(this).text());
    count++;
});

 

2.第1引数を指定

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$(".cft").each(function(n) {
console.log(n + ': ' + $(this).text());
});
$(".cft").each(function(n) { console.log(n + ': ' + $(this).text()); });
$(".cft").each(function(n) {
    console.log(n + ': ' + $(this).text());
});

 

3. 第1引数と第2引数を指定

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$(".cft").each(function(n, k) {
console.log(n + ': ' + $(k).text());
});
$(".cft").each(function(n, k) { console.log(n + ': ' + $(k).text()); });
$(".cft").each(function(n, k) {
    console.log(n + ': ' + $(k).text());
});

 

jQuery

Posted by arkgame