課程
/前端開發
/jQuery
/jQuery基礎(二)—DOM篇
DOM篇最后一節,怎么用each()方法逐個使每個li改變顏色?
2016-10-30
源自:jQuery基礎(二)—DOM篇 6-10
正在回答
????<script?type="text/javascript"> ????i?=?0; ????$("button:last").click(function()?{ ????????$("li").each(function(index,?element)?{???????? ????????????if?(index?==?i)?{ ????????????????$(this).css('color','blue'); ????????????} ????????}); ????????i++; ????}) ????</script>
?i = 0;
? ? $("button:last").click(function() {
? ? ? ? $("li").each(function(index, element) { ? ? ? ?
? ? ? ? ? ? if (index == i) {
? ? ? ? ? ? ? ? $(this).css('color','blue');
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? i++;
? ? })
思路就是加個定時器,延時變色。比如第1個li過200毫秒變成紅色,第2個li過400毫秒再變成紅色...
var?interval?=?0; $("li").each(function(index,?element)?{ ????var?$el?=?$(this); ????setTimeout(function()?{ ????????$el.css('color','red'); ????},?interval?+=?200); });
慕粉198531 提問者
舉報
jQuery第二階段開啟DOM修煉,了解創建、插入、刪除與替換
2 回答each方法用的多嗎
5 回答$("li").css('')和$("li").each有什么區別嗎?
4 回答each方法中element參數怎么使用?
2 回答直接用$("li").css("color","red")與整個each的方法效果是一樣的。有什么區別或好處么
2 回答用each()方法點擊列表彈出此列表的內容
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-12-14
2016-12-14
?i = 0;
? ? $("button:last").click(function() {
? ? ? ? $("li").each(function(index, element) { ? ? ? ?
? ? ? ? ? ? if (index == i) {
? ? ? ? ? ? ? ? $(this).css('color','blue');
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? i++;
? ? })
2016-10-30
思路就是加個定時器,延時變色。比如第1個li過200毫秒變成紅色,第2個li過400毫秒再變成紅色...