0

これは簡単な質問だと思います。引用をランダム化するJavaScriptがいくつかありますが、引用と引用の作成者を異なる色にしたいです。もちろん、CSS を使用してすべてのテキストの外観を変更することはできますが、引用と著者を区別することがうまくできず、javascript にあまり精通していません。

<script language="JavaScript">

var Quotation=new Array()

Quotation[0] = "Only two things are infinite, the universe and human stupidity, and I'm    not sure about the former.<br>Albert Einstein";
Quotation[1] = "Science is a way of thinking much more than it is a body of knowledge. <br>Carl Sagan";
Quotation[2] = "Few tragedies can be more extensive than the stunting of life, few injustices deeper than the denial of an opportunity to strive or even to hope, by a limit imposed from without, but falsely identified as lying within.<br>Stephen Jay Gould";
Quotation[3] = "I don't want to talk to you no more, you empty-headed animal food trough wiper! I fart in your general direction! Your mother was a hamster and your father smelt of elderberries!<br>Taunting Frenchman";

var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>
4

3 に答える 3

0

引用がサンプルの形式であることが確実な場合は、各引用を
タグで分割し、次のようにその周りにスパンを追加します。

var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){
  var Quote = Quotation[whichQuotation];
  var quoteColor = 'green';
  var authorColor = 'red';
  var quoteSplits = Quote.split('<br>');
  var Quote = '<span style="color:' + quoteColor + '">' + quoteSplits[0] + '</span><br>';
      Quote += '<span style="color:' + authorColor + '">' + (quoteSplits.length > 1 ? quoteSplits[1] : '') + '</span>'; 
  document.write(Quote);
}
showQuotation();
于 2013-08-21T05:20:36.047 に答える