HTML
<div class="try">
<span>G</span>
<span>d</span>
<span>d`s </span>
<span>O</span>
<span>w</span>
<span>n </span>
<span>C</span>
<span>o</span>
<span>u</span>
<span>n</span>
<span>t</span>
<span>r</span>
<span>y</span>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
.try span:nth-of-type(even){
color:green;
}
.try span:nth-of-type(odd){
color:red;
}
Run Code Online (Sandbox Code Playgroud)
如果你可以使用jQuery,你可以使用这段代码:
$( "span" ).each(function( index ) {
var originalText = $( this ).text();
var newText = "";
for( var i = 0; i < originalText.length; i++)
{
if (i % 2 === 0)
newText += "<span>" + originalText.charAt(i) + "</span>";
else
newText += originalText.charAt(i);
}
$( this ).html(newText);
});
Run Code Online (Sandbox Code Playgroud)
它取代了想要的字符与原来的跨度内的跨度,然后你就可以风格跨度.
小提琴:http://jsfiddle.net/m6RTW/