2017-04-18 44 views
-3

我尝试添加样式使用CSS和jQuery JS额外的JS从这种解决方案我的样品字最后一个字:第n-一切造型与CSS和JS

'http://codepen.io/FWeinb/pen/djuIx' 

我'能上色奇数和偶数字母,也是第n(n)个字母,但我有问题:最后一个字的例子。这是我所做的和:最后一句话不适用于我:/ my version

+2

提供一个[MCVE],请。顺便说一下,我没有downvote ...呢。 – zer00ne

+0

':: last-word'你试过这个吗? – Jai

+0

是的,我也试过,但没有积极的结果:/ – user2599820

回答

0

下面的代码将突出显示span, div, p的最后一个词,类名称为highlight_last_word

学分:https://codepen.io/mel/pen/jLEKH?css-preprocessor=none

$(function() { 
 
    $(".highlight_last_word").html(function() { 
 
    var text = $(this).text().trim().split(" "); 
 
    var last = text.pop(); 
 
    return text.join(" ") + (text.length > 0 ? " <span class='last-word'>" + last + "</span>" : last); 
 
    }); 
 
})
.last-word { 
 
    color: #F00; 
 
    font-size: 40px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<span class="highlight_last_word"> 
 
    This is sample text to color last WORD 
 
</span><br><br> 
 

 
<span class="highlight_last_word"> 
 
    Some text which could be long enought to move to next line. Some text which could be long enought to move to next line. Some text which could be long enought to move to next line. Some text which could be long enought to move to next line. 
 
</span><br><br> 
 

 
<span class="highlight_last_word"> 
 
    This is some other text 
 
</span>