我有以下五个div
<div class="timeline-axis-text timeline-axis-text-minor" style="position: absolute; left: -12px; top: 20px;">Text1</div>
<div class="timeline-axis-text timeline-axis-text-minor" style="position: absolute; left: -20px; top: 20px;">Text2</div>
<div class="timeline-axis-text timeline-axis-text-minor" style="position: absolute; left: -40px; top: 20px;">Text3</div>
<div class="timeline-axis-text timeline-axis-text-minor" style="position: absolute; left: -60px; top: 20px;">Text4</div>
<div class="timeline-axis-text timeline-axis-text-minor" style="position: absolute; left: -80px; top: 20px;">Text5</div>
我尝试$('.timeline-axis-text-minor').text()
并打印出"Text1Text2Text3Text4Text5"
,但我希望使用$('.timeline-axis-text-minor').each()
来迭代这五个元素并分别获取唯一的文字值。
答案 0 :(得分:1)
尝试
var arr = $('.timeline-axis-text-minor').map(function(){ //get array of all the texts
return $(this).text();
}).get();
使用上面的代码后获取唯一数组
arr= $.unique(arr);
获取唯一数组
var text = []; //empty array
$('.timeline-axis-text-minor').each(function () {
var val = $(this).text();
if (text.indexOf(val) === -1) text.push(val); //if it not in the array than add it to array
});
答案 1 :(得分:1)
这样做:
$('.timeline-axis-text-minor').each(function(){
var uniqueText = $(this).text();
});
答案 2 :(得分:1)
试试这个:
$('.timeline-axis-text-minor').each(function(){
alert($(this).text());
});
答案 3 :(得分:1)
$('.timeline-axis-text-minor').map(function(){return $(this).text(); }).get().join("$");
答案 4 :(得分:1)
试试这个
var arr = []
$('.timeline-axis-text-minor').each(function () {
arr.push($(this).text());
});
console.log(arr)
OR
如果你想分开
$('.timeline-axis-text-minor:eq(0)').text() //Return Text 1