2011-12-16 38 views

回答

2

你可以做同样的jQuery的:

$('div').each(function() { 
    if ($(this).attr('id') == '1') 
     $(this).click(function() { // handler for first div }); 

    if ($(this).attr('id') == '2') 
     $(this).click(function() { // handler for second div });\ 

    ... 
}); 
0

$('div')

这将创建的DIV的一个jQuery集合Docs

5

要做到这一点jQuery的使用:

$("div").each(function() { 
    // 'this' is the div 
}); 

这是相同:

var divs = document.getElementsByTagName('div'), 
    i, 
    len, 
    div; 

for (i = 0, len = divs.length; i < len; i++) { 
    div = divs[i]; 
} 
0
var divs = jQuery('div'); 

divs.each(function(){ 
    $(this).DoSomeThing(); 

    // $(this) refers to a div 
}); 
相关问题