2017-04-18 48 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>