簡體   English   中英

使用.text()將字母/數字序列插入多個子span標記。 有沒有更有效的方法?

[英]Inserting a letter/number sequence to multiple child span tags using .text(). Is there a more efficient way?

我需要使用特定的字母來插入多個span標簽,也許要說20個。 我需要20行.text()還是有更有效的方法? 謝謝。

$(document).ready(function(){
    $(".owl-page:first-child span").text("A");
    $(".owl-page:nth-child(2) span").text("C");
    $(".owl-page:nth-child(3) span").text("D");
    $(".owl-page:nth-child(4) span").text("E");
    $(".owl-page:nth-child(5) span").text("F");
});

與其他答案類似,但方法略有不同:

JS小提琴

var letters = {
    1: 'A',
    2: 'B',
    3: 'C',
    4: 'D',
    5: 'E',
    6: 'F'
}

$.each(letters, function(e) {
  $('.owl-page:nth-child(' + e + ') span').text(this);
});

您可以將字母放入數組中,並與.each()方法一起使用。

var letters = ['A', 'C', 'D', 'E', 'F'];
var counter = 0;

$(".owl-page > * span").each(function(){ 
    if(counter >= letters.length) 
        return false; 
    $(this).text(letters[counter]);
    counter++;
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM