2

我为 FreeCodeCamp 创建的随机报价机几乎完成了,但我想将每个数组项的第二部分斜体。我查了一下,发现没有为我工作。有没有更简单的方法来定位每个项目的第二部分......我认为这可能很困难,但我还很新。谢谢!

    var quote = [
    "I could die for you. But I couldn't, and wouldn't, live for you." + "  —     Ayn Rand, The Fountainhead",
"There is not love of life without despair about life." + "  — Albert Camus, The Stranger",
"Every act of rebellion expresses a nostalgia for innocence and an appeal to the essence of being." + "  — Albert Camus, The Rebel: An Essay on Man in Revolt",
"Man is always prey to his truths. Once he has admitted them, he cannot free himself from them." + "  — Albert Camus, The Myth of Sisyphus and Other Essays",
"There is nothing outside of yourself that can ever enable you to get better, stronger, richer, quicker, or smarter. Everything is within. Everything exists. Seek nothing outside of yourself." + 
"  — Miyamoto Musashi, The Book of Five Rings",
"When one person suffers from a delusion, it is called insanity. When many people suffer from a delusion it is called a Religion." + "  — Robert M. Pirsig, Zen and the Art of Motorcycle Maintenance: An Inquiry Into Values",
"The books that the world calls immoral are books that show the world its own shame." + "  — Oscar Wilde, The Picture of Dorian Gray",
"Rather than love, than money, than fame, give me truth." + "  — Henry David Thoreau, Walden",
"For a friend with an understanding heart is worth no less than a brother" + "  — Homer, The Odyssey",
"You have power over your mind - not outside events. Realize this, and you will find strength." + "  — Marcus Aurelius, Meditations",
"The happiness of your life depends upon the quality of your thoughts." + "  — Marcus Aurelius, Meditations",
"Everything we hear is an opinion, not a fact. Everything we see is a perspective, not the truth." + "  — Marcus Aurelius, Meditations",
"It's the job that's never started as takes longest to finish." + "  — J.R.R. Tolkien, The Lord of the Rings",
"History is a wheel, for the nature of man is fundamentally unchanging. What has happened before will perforce happen again." + "  — George R.R. Martin, A Feast for Crows" ,
"The first method for estimating the intelligence of a ruler is to look at the men he has around him." + "  — Niccolò Machiavelli, The Prince",
"Time makes more converts than reason." + "  — Thomas Paine, Common Sense",
];


  function showQuote(num) {
    return quote[num];
  }

  $(document).ready(function() {
    $('#newQuote').click(function() {
      $('#quoteHere').html(showQuote(Math.floor(Math.random() * 16)));
    });
  });
4

4 回答 4

1

为了简单起见,您可以使用 str.italics(); 在您的 js 中使用斜体。这不是最佳做法。但为了您的问题,可以这样做。我做了一个快速的 jsfiddle,展示了我为修改你的字符串数组所做的事情。我使用子字符串来定位每个字符串中的 - 来拆分作者和引用。

   var quote = [
    "I could die for you. But I couldn't, and wouldn't, live for you." + "  —     Ayn Rand, The Fountainhead",
"There is not love of life without despair about life." + "  — Albert Camus, The Stranger",
"Every act of rebellion expresses a nostalgia for innocence and an appeal to the essence of being." + "  — Albert Camus, The Rebel: An Essay on Man in Revolt",
"Man is always prey to his truths. Once he has admitted them, he cannot free himself from them." + "  — Albert Camus, The Myth of Sisyphus and Other Essays",
"There is nothing outside of yourself that can ever enable you to get better, stronger, richer, quicker, or smarter. Everything is within. Everything exists. Seek nothing outside of yourself." + 
"  — Miyamoto Musashi, The Book of Five Rings",
"When one person suffers from a delusion, it is called insanity. When many people suffer from a delusion it is called a Religion." + "  — Robert M. Pirsig, Zen and the Art of Motorcycle Maintenance: An Inquiry Into Values",
"The books that the world calls immoral are books that show the world its own shame." + "  — Oscar Wilde, The Picture of Dorian Gray",
"Rather than love, than money, than fame, give me truth." + "  — Henry David Thoreau, Walden",
"For a friend with an understanding heart is worth no less than a brother" + "  — Homer, The Odyssey",
"You have power over your mind - not outside events. Realize this, and you will find strength." + "  — Marcus Aurelius, Meditations",
"The happiness of your life depends upon the quality of your thoughts." + "  — Marcus Aurelius, Meditations",
"Everything we hear is an opinion, not a fact. Everything we see is a perspective, not the truth." + "  — Marcus Aurelius, Meditations",
"It's the job that's never started as takes longest to finish." + "  — J.R.R. Tolkien, The Lord of the Rings",
"History is a wheel, for the nature of man is fundamentally unchanging. What has happened before will perforce happen again." + "  — George R.R. Martin, A  Feast for Crows" ,
"The first method for estimating the intelligence of a ruler is to look at the men he has around him." + "  — Niccolò Machiavelli, The Prince",
"Time makes more converts than reason." + "  — Thomas Paine, Common Sense",
];
for (var i = 0; i < quote.length; i ++) {
  var temp = quote[i];
  var index  = temp.indexOf("—");
  var firstHalf = temp.substring(0, index);
  temp = temp.substring(index, temp.length);
  temp = temp.italics();
  quote[i] = firstHalf + temp;
}

  function showQuote(num) {

    return quote[num];
  }

  $(document).ready(function() {
    $('#newQuote').click(function() {
      $('#quoteHere').html(showQuote(Math.floor(Math.random() * 16)));
    }); 
  });

https://jsfiddle.net/6ctcaqrr/

于 2016-04-27T20:54:59.447 回答
1

您可能会发现使用对象分别处理记录的不同部分更容易:

var quotes = [
    {
        quote: "When one person suffers from a delusion, it is called insanity. When many people suffer from a delusion it is called a Religion.",
        author: "Robert M. Pirsig, Zen and the Art of Motorcycle Maintenance: An Inquiry Into Values"
    }
];

// Pick a quote object using a random index
var selectedQuote = quotes[Math.floor(random()*quotes.length)];

// Render the quote into an HTML element
document.getElementById("quoteDisplay").innerHTML = selectedQuote.quote + " - <i>" + selectedQuote.author + "</i>";

通过这种方式,您将关注点分开:数据与表示分开存储。

如果您想区分作者和作品,您甚至可以更进一步:

var quotes = [
    {
        quote: "When one person suffers from a delusion, it is called insanity. When many people suffer from a delusion it is called a Religion.",
        author: "Robert M. Pirsig",
        book: "Zen and the Art of Motorcycle Maintenance: An Inquiry Into Values"
    }
];
于 2016-04-27T20:44:21.673 回答
0

您可以将文本包装在任意 HTML 元素中,并使用 CSS 或内联样式使文本变为斜体。例如

<span class="italic">When one person suffers from a delusion ... converts than reason</span>

在你的 CSS

span.italic {
    font-style: italic;
}

或者

<span style="font-style: italic;">When one person suffers from a delusion ... converts than reason</span>

于 2016-04-27T20:37:25.113 回答
0

正如 Pointy 所说,斜体是翻译细节,因此您需要在引用的第二部分中包含标签或font-style按照 Ozrix 的建议使用。要使用您当前的选择器实现这一点,您可以尝试以下操作:

var quote = [
  ["I could die for you. But I couldn't, and wouldn't, live for you."," — Ayn Rand, The Fountainhead"]
...];

function showQuote(num) {
  return quote[num];
}

$(document).ready(function() {
  $('#newQuote').click(function() {
    var randomNum = Math.floor(Math.random() * 16);

    $('#quoteHere').html(showQuote(randomNum)[0]);
    $('#italicsHere').html(showQuote(randomNum)[1]);
  });
});

您想将引号的两个部分拆分为数组,每个数组在主数组中包含两个字符串。

于 2016-04-27T20:44:47.593 回答