如何在加上运算符后选择数组元素的一部分

时间:2016-04-27 20:24:09

标签: javascript italics

我几乎完成了我为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 个答案:

答案 0 :(得分: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"
    }
];

答案 1 :(得分: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/

答案 2 :(得分: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>

答案 3 :(得分:0)

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

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]);
  });
});

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