Javascript 把一个单词换成不同的颜色

Javascript 把一个单词换成不同的颜色,javascript,text,colors,Javascript,Text,Colors,所以我有这个代码: <script type="text/javascript"> document.getElementById("randoBtn").addEventListener("click", clickHandler); function clickHandler(event) { event.preventDefault(); document.getElementById("message1").innerHTML = "Try writ

所以我有这个代码:

<script type="text/javascript">
    document.getElementById("randoBtn").addEventListener("click", clickHandler);

function clickHandler(event) {
    event.preventDefault();
    document.getElementById("message1").innerHTML = "Try writing a " + selectedWord() + " and";
    var selectWord  
    document.getElementById("message2").innerHTML = selectedWord() + " story";
}

function selectedWord(){
    var myarray = new Array("Action", "Adventure", "Apocalyptic", "Comedy", "Detective", "Disaster", "Drama", "Epic", "Erotica", "Fable", "Fairy Tale", "Fantasy", "Fiction", "Folk Tale", "Ghost", "Historical", "Horror", "Humor", "Military", "Mystery", "Mythology", "Narrative", "Parallel Universe", "Paranormal", "Parody", "Poetry", "Post-Apocalyptic", "Pulp Fiction", "Quest", "Realistic Fiction", "Revenge", "Romantic", "Science Fiction", "Short Story", "Spy", "Supernatural", "Tall Tale", "Thriller", "Time Travel", "Undersea", "Western");

    return myarray[Math.round(Math.random() * (myarray.length - 1))];
}   
</script>

document.getElementById(“randoBtn”).addEventListener(“单击”,clickHandler);
函数clickHandler(事件){
event.preventDefault();
document.getElementById(“message1”).innerHTML=“尝试编写“+selectedWord()+”和”;
变量选择字
document.getElementById(“message2”).innerHTML=selectedWord()+“故事”;
}
函数selectedWord(){
var myarray=新数组(“动作”、“冒险”、“天启”、“喜剧”、“侦探”、“灾难”、“戏剧”、“史诗”、“情色”、“寓言”、“童话”、“幻想”、“小说”、“民间故事”、“幽灵”、“历史”、“恐怖”、“幽默”、“军事”、“神秘”、“神话”、“叙事”、“平行宇宙”、“超自然”、“戏仿”、“诗歌”、“后天启”,“低俗小说”、“探索”、“现实主义小说”、“复仇”、“浪漫”、“科幻”、“短篇小说”、“间谍”、“超自然”、“故事情节”、“惊悚片”、“时间旅行”、“海底”、“西部”);
返回myarray[Math.round(Math.random()*(myarray.length-1));
}   

是否需要将selectWord()变量更改为其他颜色?我只希望它与文本的其余部分相对突出。提前感谢!

这里,我用一个
标记包装了selectedWord返回,给出了一个类名称,并设置了类名称的样式

函数clickHandler(事件){
event.preventDefault();
document.getElementById(“message1”).innerHTML=“尝试编写“+selectedWord()+”和”;
变量选择字
document.getElementById(“message2”).innerHTML=”“+selectedWord()+“story”;
}
函数selectedWord(){
var myarray=新数组(“动作”、“冒险”、“启示录”、“喜剧”、“侦探”、“灾难”、“戏剧”、“史诗”、“情色”、“寓言”、“童话”、“幻想”、“小说”、“民间故事”、“鬼”、“历史”、“恐怖”、“幽默”、“军事”、“神秘”、“神话”、“叙事”、“平行宇宙”、“超自然”、“戏仿”、“诗歌”、“后启示录””低俗小说、探索小说、现实主义小说、复仇小说、浪漫小说、科幻小说、短篇小说、间谍小说、超自然小说、长篇小说、惊悚片、时间旅行、海底小说、西部小说);
返回myarray[Math.round(Math.random()*(myarray.length-1));
}  
window.onload=function(){document.getElementById(“randobN”).addEventListener(“单击”,clickHandler);}
.Words{颜色:红色;字体重量:粗体;}
开始!

非常好用!谢谢。