我正在编写一个应用程序,它接受用户输入并随机将颜色分配给文本中的每个字母。JavaScript:随机选择颜色作为基于输入的字母
HTML代码
<label id="text">
Retrieve data from selected text input among various text inputs with same
name & id upon hitting Enter
</label>
<div>
<input type="text" id="inputColors">
</div>
<input id="Enter" type="button" value="Enter" />
的JavaScript
document.getElementById("Enter").onclick = function() {
var colors = document.getElementById('inputColors').value;
var colorslist = colors.split(/[;,]+/);
$('#text').each(function() {
$(this).html($(this).text().split(' ').map(function(v) {
return '<span style="color:' + colorslist[Math.floor(Math.random() *
colorslist.length)] + '">' + v + '</span>';
}).join(' '));
});
}
我能颜色的每个字,但希望它这样做对每个letter.I不想使用CSS。
'.split( '')'代替'.split(”“)',很明显。 '.join'一样。 –