简体   繁体   English

在javascript变量中,如何更改单行的字体颜色?

[英]From within a javascript var, how do you change the font color for individual lines?

I'm sure this is an easy question. 我敢肯定这是一个简单的问题。 I have some javascript that randomizes quotes, but I want the quote and the author of the quote to be different colors. 我有一些随机化引号的JavaScript,但我希望引号和引号的作者是不同的颜色。 I can of course use CSS to change the appearance of all the text, but I am not having much luck separating the quote and the author and I am not very proficient with 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>

Just enclose parts of text you want separate colors for into <span></span> tags with different styles. 只需将您想要单独使用颜色的部分括入具有不同样式的<span></span>标签中。

Eg 例如

Quotation[0] = "<span style='color:green'>Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.</span><br><span style='color:red'>Albert Einstein</span>";

Let the quotes and the Authors be in HTML itself with different spans and common class for each and then apply styles to each class via jQuery. 让引号和作者在HTML本身中使用不同的跨度和通用类,然后通过jQuery将样式应用于每个

Here's an example : 这是一个例子:

<span class="jQuote">Some blah blah stuff</span> <br />
<span class="jAuthor">Mr. Somebody</span>

In Javascript : 在Javascript中:

$(function(){
  $(".jQuote").css(/*Whatever styles you need to apply*/);
  $(".jAuthor").css(/*Whatever styles you need to apply*/);
});  

Find more on usage of css via jQuery. 通过jQuery查找有关CSS用法的更多信息。

But a better approach would be to apply different classes to quotes and authors and apply styles via CSS. 但是更好的方法是将不同的类应用于引号和作者,并通过CSS应用样式。

If quotes are sure to be in the format you have samples from, then split each quote by 如果确定引号的格式与您的样本相同,则用
tag and append spans around it as follows: 标记并附加跨度,如下所示:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM