2017-10-04 64 views
-1

我写了两个函数用于随机更改背景的颜色和报价的内容。接下来,我点击了一个按钮来执行这些功能。 我成功改变了颜色,但引号不会出现,并且当程序中的两个函数都不会改变颜色。 我的代码有什么问题?报价颜色或内容不显示

<!DOCTYPE HTML> 
<html> 




<head> 
<meta charset="UTF-8"> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<!-- let's get started --> 
<title>Random Quote</title> 

<!-- //******CSS********************// --> 
<style> 
    .Container{ 
    height:100%; 

    display:grid; 

    background-color:white; 
    } 
</style> 
<!-- //******JavaScript************// --> 
<script> 
function randColor() { 
    var letters = 'ABCDEF'.split(''); 
    var color = '#'; 
    for (var i = 0; i < 6; i++) {color += letters[Math.round(Math.random() * 15)]; 
    } 
    if(color=="#000000"){ // to exclude white from brower background color since the color of the 'Container' is always white  
    color[2]=color[2]+1; 
    document.body.style.backgroundColor = color;} 
    else 
    document.body.style.backgroundColor = color;  
} 

function randQuote(){ 
    var randNum= Math.floor(Math.random()*10) ; 
    var quote=[ 
    "You can do anything, but not everything. —David Allen", 
    "Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.—Antoine de Saint-Exupéry", 
    "The richest man is not he who has the most,but he who needs the least. —Unknown Author", 
    "You miss 100 percent of the shots you never take. —Wayne Gretzky", 
    "You must be the change you wish to see in the world. —Gandhi", 
    "We are what we repeatedly do. excellence, then, is not an act but a habit.—Aristotle" 
    "A wise man gets more use from his enemies than a fool from his friends.—Baltasar Gracian", 
    "What we think, or what we know, or what we believe is, in the end, of little consequence. The only consequence is what we do.—John Ruskin", 
    "The real voyage of discovery consists not in seeking new lands but seeing with new eyes.—Marcel Proust", 
    "Don\’t ever wrestle with a pig. You’ll both get dirty, but the pig will enjoy it.—Cale Yarborough" 
    ]; 

    document.getElementById("ShowQuote").innerHTML=quote[randNum]; 
} 

</script> 
</head> 

<!-- //******HTML******************// --> 
<body> 

<div class="Container"> 
    <div id="ShowQuote"> 

    </div> 

    <button class="btn" onclick="randQuote();randColor()">New Quote</button> 





</div> 

</body> 




</html> 
+0

你的HTML是无效的。通过验证器运行您的HTML并首先修复您的错误。 – Rob

+0

谢谢,我解决了它,但仍不知道如何激活这2个功能。 –

回答

0

您在第46行缺少一个逗号。这在开发人员工具的浏览器控制台中显示为错误。

Uncaught SyntaxError: Unexpected string

只需在该行的末尾添加一个逗号:

"We are what we repeatedly do. excellence, then, is not an act but a habit.—Aristotle", 
+0

哇它的工作。感谢一百万Rob。你真了不起。对我来说,抓住这些错误还不容易。 –