简体   繁体   English

如何遍历类并在数组中添加文本

[英]How to loop through classes and add text in array

I'm trying to make a function that ellipsis the text, given a number of max letters. 我正在尝试创建一个省略文本的函数,给出一些最大字母。

I got an array with all the text of the classes, already formated the way i want. 我得到了一个包含所有类的文本的数组,已经按照我想要的方式编写了。 The problem is that i need to change the text() of every class with the text that is in the array. 问题是我需要使用数组中的文本更改每个类的text()。

Here is my code: 这是我的代码:

var array = $('.elipse').map(function(){
    return $(this).text();
}).get();


var i;
var teste = [];

for (i=0;i<array.length;i++){
    if (array[i].length > 30){

    teste.push(array[i].substr(0,10));

    } else {
        teste.push(array[i]);
    }
}

for (var i=0;i<teste.length;i++){

     $('.elipse').each(function(){

      $(this).text(teste[i]);

    });

  } 

The problem is in the last for loop. 问题出在最后一个for循环中。 Every text of every element that contains the elipse class must be changed to the text in teste array. 必须将包含elipse类的每个元素的每个文本更改为teste数组中的文本。 I tried to loop it in a lot of different ways, but im missing something 我尝试以很多不同的方式循环它,但我错过了一些东西

Just loop the elements only, else you're overwriting: 只是循环元素,否则你要覆盖:

$('.elipse').each(function(i){

  $(this).text(teste[i]);

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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