报价脚本不起作用

时间:2017-10-18 17:09:09

标签: javascript html

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

// Manually generate Base 64 encoding of username and password
String encodedAuthString = "Basic " + ("$username:$password".bytes.encodeBase64().toString())

// Set in the `HttpBuilder.configure` closure
request.headers['Authorization'] = encodedAuthString
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;

2 个答案:

答案 0 :(得分:2)

aquote[1]行有语法错误。最后,它应该是Focus on science.\"";而不是Focus on science."\"";。请注意最后的第三个引用。

修复后,您需要设置innerText的{​​{1}},而不是其值。请参阅下面的工作代码:

quote

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