1种 通过each遍历li 可以获得所有li的内容 11a 22b 33c 44d 55e 输出每个li值 // 1种 通过each遍历li 可以获得所有li的内容 $("button").click(function(){ $(".one > li").each(function(){ // 打印出所有li的内容 console.log($(this).text()); }) }); 로그인 후 복사2种 通过each遍历li 通过$(this)给每个li加事件 2222 22b 3333 44d 5555 // 2种 通过each遍历li 通过$(this)给每个li加事件 $('.two > li').each(function(index) { console.log(index +":" + $(this).text()); // 给每个li加click 点那个就变颜色 $(this).click(function(){ alert($(this).text()); $(this).css("background","#fe4365"); }); }); 로그인 후 복사4种 遍历所有li 给所有li添加 class类名 Eat Sleep 3种 点击3 // 4种 遍历所有li 给所有li添加 class类名 $('span').click(function(){ $('.ctn3 > li').each(function(){ $(this).toggleClass('example'); }) }); 로그인 후 복사5种 在each()循环里 element == $(this) each练习2 div { width: 40px; height: 40px; margin: 5px; float: left; border: 2px blue solid; text-align: center; } span { width: 40px; height: 40px; color: red; } Stop here Change colors // 在each()循环里 element == $(this) $('button').click(function(){ $('div').each(function(index,element){ //element == this; $(element).css("background","yellow"); if( $(this).is("#stop")){ $('span').text("index :" + index); return false; } }) }) 로그인 후 복사以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持PHP中文网! 更多深入理解jquery中的each用法相关文章请关注PHP中文网!