gpt4 book ai didi

javascript 数组仅在未完全充满内容时才有效

转载 作者:行者123 更新时间:2023-12-02 18:58:16 25 4
gpt4 key购买 nike

我有一个充满引号的数组,加载时会显示随机引号。我遇到的问题是,当我只加载 2 个引号时,一切正常,但当我添加整个列表时,它不再运行。

这是有效的代码-

<div id="quotes">Quotes</div>


<script type="text/javascript">

var quotes = [
"<i>Some people feel the rain. Others just get wet.</i><br><b>Bob Marley</b>",
"<i>Do not pray for an easy life, pray for the strength to endure a difficult one. </i><br><b>Bruce Lee</b>"

];

document.getElementById('quotes').innerHTML = quotes[Math.floor(Math.random() * quotes.length)];
</script>

上面的内容按预期工作,但是当我添加其余引号(如下所示)时,脚本不再工作。

<div id="quotes"></div>

<script type="text/javascript">
var quotes = [
"<i>Some people feel the rain. Others just get wet.</i><br><b>Bob Marley/b>",
"<i>Do not pray for an easy life, pray for the strength to endure a difficult one. </i><br><b>Bruce Lee</b>",
"<i>Success is not final, failure is not fatal: it is the courage to continue that counts.</i><br><b>Winston
Churchill</b>",
"<i>If your dreams don’t scare you they’re not big enough.</i><br><b></b>",
"<i>It takes courage to grow up and become who you really are.</i><br><b>E.E. Cummings</b>",
"<i>All endings are also beginnings, we just don’t know it yet.</i><br><b></b>",
"<i>There are three kinds of people: Those who make it happen, those who watch it happen, and
those who wonder what the heck happened.</i><br><b></b>",
"<i>There are people so poor, that the only thing they have is money.</i><br><b> </b>",
"<i>Things do not happen. Things are made to happen.</i><br><b>John F. Kennedy</b>",
"<i>Destiny is a name often given in retrospect to choices that had dramatic consequences.</i><br><b>J.K. Rowling</b>",
"<i>When I was 5 years old, my mother always told me that happiness was the key to life.
When I went to school, they asked me what I wanted to be when I grew up. I wrote
down ‘happy’. They told me I didn’t understand the assignment, and I told them they didn’t
understand life.</i><br><b>John Lennon</b>",
"<i>Not all those who wander are lost</i><br><b>JRR Tolkien</b>",
"<i>We all die. The goal isn’t to live forever, the goal is to create something that will.</i><br><b></b>",
"<i>Strive for progress, not perfection.</i><br><b></b>",
"<i>What defines us is how well we rise after falling.</i><br><b></b>",
"<i>It’s not hard to make decisions once you know what your values are.</i><br><b>Roy E. Disney</b>",
"<i>Sorry’s not good enough.</i><br><b></b>",
"<i>I may not be there yet, but I’m closer than I was yesterday.
Every day is a new beginning. Stay away from what might have been and look at what can
be.</i><br><b></b>",
"<i>Who inspires you?</i><br><b></b>",
"<i>If you play by the rules long enough, then you can change the game.</i><br> <b>Enders Game</b>"
];

document.getElementById('quotes').innerHTML = quotes[Math.floor(Math.random() * quotes.length)];
</script>

最佳答案

JavaScript 字符串中不能有换行符。您需要更改此设置:

"str part
the rest"

对此:

"str part\nthe rest"

"str part\n"
+ "the rest"

您应该检查 JavaScript 控制台是否有错误;这可以帮助您调试

关于javascript 数组仅在未完全充满内容时才有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15080756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com