简体   繁体   中英

Wrapping Text Inside List Element JQuery

I want to wrap every text inside a list element with a tag. The following code only ever changes the last list element. I can't seem to figure it out.

jQuery:

var boxlistAr = jQuery('ul.box-list li').toArray();

for (var i = 0; i < boxlistAr.length; i++){
    var text = jQuery(boxlistAr[i]).text();
    jQuery(boxlistAr[i]).html('<span class="li-text">'+text+'</span>');
}

HTML:

<ul class="box-list">
              <li><span class="li-text">Inspirational opening ceremony</li>
              <li><span class="li-text">Safety briefing</li>
              <li><span class="li-text">Equipment tuning</li>
              <li><span class="li-text">Luggage drop</li>
              <li><span class="li-text">Refreshment pitstops every 20-30k</li>
            </ul>

使用jQuery的wrap()方法。

$("ul.box-list li").wrap($("<span>").addClass("li-text"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM