我尝试使用CSS和Jquery JS以及此解决方案中的其他JS为我的示例单词添加样式:
'http://codepen.io/FWeinb/pen/djuIx'
我能够为ODD和EVEN字母以及第n(n)个字母着色,但我有问题:最后一个字例子。这就是我所做的:最后一句话对我不起作用:/ my version
答案 0 :(得分: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>