JS的循環(huán)賦class值

admin HTML/CSS/JS評(píng)論7221字?jǐn)?shù) 429閱讀模式

例如

  • 1
  • 2
  • ….. 我要給第一個(gè)li附上class=“01”,給第二個(gè)li附上class=“02”接下來(lái)的以此類推 對(duì)應(yīng)的JS寫(xiě)法與JQ寫(xiě)法該怎么寫(xiě)文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.strong-digital.cn/7680.html

    1. js:  
    2.    
    3. window.onload = function(){  
    4.     var lis = document.getElementsByTagName("li");  
    5.     for (var i = 0; i < lis.length; i++) {  
    6.         lis[i].className = String(i).length > 1 ? i : "0" + i;  
    7.     }  
    8. }  
    9.    
    10. jquery:  
    11.    
    12. jQuery(function($){  
    13.     $("li").each(function(i, domEle){  
    14.         $(this).addClass(String(i).length > 1 ? i : "0" + i);  
    15.     });  
    16. });  
    文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.strong-digital.cn/7680.html文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.strong-digital.cn/7680.html
    版權(quán)聲明:文章圖片資源來(lái)源于網(wǎng)絡(luò),如有侵權(quán),請(qǐng)留言刪除!!!
    廣告也精彩
    admin
    • 本文由 發(fā)表于 2021年9月25日 18:49:53
    • 轉(zhuǎn)載請(qǐng)務(wù)必保留本文鏈接:http://www.strong-digital.cn/7680.html
    匿名

    發(fā)表評(píng)論

    匿名網(wǎng)友 填寫(xiě)信息

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: