2017-10-18 102 views
2

我有一个脚本,点击按钮后显示随机引号。请帮我理解它为什么不起作用。行情脚本不起作用

function quotes() { 
 
    var aquote = new Array; 
 
    aquote[0] = "\"Nobody exists on purpose. Nobody belongs anywhere. We're all 
 
    going to die.Come watch TV.\""; 
 
    aquote[1] = "\"Listen, Morty, I hate to break it to you but what people call 
 
    love is just a chemical reaction that compels animals to breed.It hits 
 
    hard, Morty, then it slowly fades, leaving you stranded in a failing 
 
    marriage.I did it.Your parents are gonna do it.Break the cycle, Morty. 
 
    Rise above.Focus on science."\""; 
 
    aquote[2] = "\"Weddings are basically funerals with cake.\"" 
 
    aquote[3] = "\"There is no God, Summer. Gotta rip that band-aid off now 
 
    you’ ll thank me later.\"" 
 
    aquote[4] = "\"I’m sorry, but your opinion means very little to me.\"" 
 
    aquote[5] = "\"Being nice is something stupid people do to hedge their 
 
    bets.\"" 
 
    
 
    rdmQuote = Math.floor(Math.random() * aquote.length); 
 
    document.getElementById("quote").value = aquote[rdmQuote]; 
 
} 
 
window.onload = quotes;
<marquee><p id="quote"></p></marquee>

+2

的可能的复制[声明一个字符串VAR在JavaScript中多行/jQuery](https://stackoverflow.com/questions/21202782/declare-a-string-var-with-multiple-lines-in-javascript-jquery) – CBroe

+1

另一种可能的重复:[“.innerHTML”和“ .value“in JS](https://stackoverflow.com/q/31225901/8173752) –

+1

JavaScript中的文本文字不能只从一行到另一行 - 在这方面检查重复。你自己用你的浏览器的开发工具,特别是控制台 - 它已经告诉你,你在那个部分有一个错误,所以你可能没有希望更有目的地研究这一点。 – CBroe

回答

2

您对aquote[1]行语法错误。最后它应该是Focus on science.\"";而不是Focus on science."\"";。注意结尾的第三个引号。

一旦修复,您需要设置innerTextquote,而不是其值。请参见下面的工作片段:

<script> 
 
function quotes(){ 
 
var aquote = new Array; 
 
    aquote[0]="\"Nobody exists on purpose. Nobody belongs anywhere. We're all going to die. Come watch TV.\""; 
 
    aquote[1]="\"Listen, Morty, I hate to break it to you but what people call love is just a chemical reaction that compels animals to breed. It hits hard, Morty, then it slowly fades, leaving you stranded in a failing marriage. I did it. Your parents are gonna do it. Break the cycle, Morty. Rise above. Focus on science.\""; 
 
    aquote[2]="\"Weddings are basically funerals with cake.\"" 
 
    aquote[3]="\"There is no God, Summer. Gotta rip that band-aid off now you’ll thank me later.\"" 
 
    aquote[4]="\"I’m sorry, but your opinion means very little to me.\"" 
 
    aquote[5]="\"Being nice is something stupid people do to hedge their bets.\""; 
 
rdmQuote = Math.floor(Math.random()*aquote.length); 
 
document.getElementById("quote").innerText=aquote[rdmQuote]; 
 
} 
 
window.onload=quotes; 
 
</script> 
 

 

 
<marquee><p id="quote"></p></marquee>

0

我看到一对夫妇的问题。第一个换行符会让你声明值的地方变得混乱。其次,你有一个额外的“标记的行之一,最后,你需要改变的innerHTML不是值试试这个:。

function quotes() { 
var aquote = new Array; 
    aquote[0]="\"Nobody exists on purpose. Nobody belongs anywhere. We're all going to die. Come watch TV.\""; 
    aquote[1]="\"Listen, Morty, I hate to break it to you but what people call love is just a chemical reaction that compels animals to breed. It hits hard, Morty, then it slowly fades, leaving you stranded in a failing marriage. I did it. Your parents are gonna do it. Break the cycle, Morty. Rise above. Focus on science.\""; 
    aquote[2]="\"Weddings are basically funerals with cake.\""; 
    aquote[3]="\"There is no God, Summer. Gotta rip that band-aid off now you’ll thank me later.\""; 
    aquote[4]="\"I’m sorry, but your opinion means very little to me.\""; 
    aquote[5]="\"Being nice is something stupid people do to hedge their bets.\""; 
rdmQuote = Math.floor(Math.random()*aquote.length); 
document.getElementById("quote").innerHTML=aquote[rdmQuote]; 
} 

window.onload=quotes; 
+1

请注意,建议避免使用“选取框”: “此功能已过时,虽然它可能在某些浏览器中仍然有效,但由于它可能随时被删除,所以不鼓励使用它,请尽量避免使用它。 “ [MDN(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/marquee) –