我对Java语言非常陌生,并且在使用多个按钮时遇到问题。当我只有第一个按钮时,它可以正常工作,但是,当我为第二个按钮添加javascript时,这两个按钮都不起作用!我认为html没有问题,但是为了安全起见,我将其包括在内。
JavaScript部分:
var quotes = [
'Associate yourself with men of good quality if you esteem your own reputation for tis
better to be alone than in bad company. - George Washington',
'To be good, and to do good, is all we have to do. -John Adams' ,
'Nothing can stop the man with the right mental attitude from achieving his goal; nothing on
earth can help the man with the wrong mental attitude. -Thomas Jefferson',
'If your actions inspire others to dream more, learn more, do more and become more, you are
a leader - John Quincy Adams',
'Any man worth his salt will stick up for what he believes right, but it takes a slightly
better man to acknowledge instantly and without reservation that he is in error. -Andrew
Jackson',
'While men inhabiting different parts of this vast continent cannot be expected to hold the
same opinions, they can unite in a common objective and sustain common principles. -Franklin
Pierce',
'In the time of darkest defeat, victory may be nearest. -William Mckinley',
'Theres good in everybody. Boost. Dont knock -Warren G. Harding',
'Pessimism never won any battle. -Dwight D. Eisenhower',
'Without passion you dont have energy, without energy you have nothing. -Donald J. Trump'
]
var countries = [
'Amennia' + '<a href = "https://en.wikipedia.org/wiki/Armenia">Wiki</a>',
'Swaziland'
'Canada'
]
function newQuote() {
var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
};
function newCountry() {
var randomNumber2 = Math.floor(Math.random() * (countries.length)):
document.getElementById('countryDisplay').innerHTML =countries[randomNumber2];
};
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>quote gen</title>
</head>
<body>
<h1>simple quote generator</h1>
<div id ="quoteDisplay">
</div>
<button onclick ="newQuote()"> New Quote</button>
<div id="countryDisplay">
</div>
<button onclick ="newCountry()"> New Country</button>
<script src="javascript.js"></script>
</body>
</html>
答案 0 :(得分:0)
这是您的有效代码。您有许多syntax error
和typos
。
您没有正确编写arrays
(引号)和(国家)内容,因此您的代码无法正常工作。同样,您也不需要使用;
来关闭函数。
您可以了解有关JS数组here
的更多信息两个按钮均有效,并显示Math.Random
运行下面的代码片段以查看其工作情况。
var quotes = [
`Associate yourself with men of good quality if you esteem your own reputation for tis
better to be alone than in bad company. - George Washington`,
`To be good, and to do good, is all we have to do. -John Adams`,
`Nothing can stop the man with the right mental attitude from achieving his goal; nothing on
earth can help the man with the wrong mental attitude. -Thomas Jefferson`,
`If your actions inspire others to dream more, learn more, do more and become more, you are
a leader - John Quincy Adams`,
`Any man worth his salt will stick up for what he believes right, but it takes a slightly
better man to acknowledge instantly and without reservation that he is in error. -Andrew
Jackson`,
`While men inhabiting different parts of this vast continent cannot be expected to hold the
same opinions, they can unite in a common objective and sustain common principles. -Franklin
Pierce`,
`In the time of darkest defeat, victory may be nearest. -William Mckinley`,
`Theres good in everybody. Boost. Dont knock -Warren G. Harding`,
`Pessimism never won any battle. -Dwight D. Eisenhower`,
`Without passion you dont have energy, without energy you have nothing. -Donald J. Trump`
]
var countries = [
'Amennia' + '<a href = "https://en.wikipedia.org/wiki/Armenia">Wiki</a>',
'Swaziland',
'Canada'
]
function newQuote() {
var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
}
function newCountry() {
var randomNumber2 = Math.floor(Math.random() * (countries.length));
document.getElementById('countryDisplay').innerHTML = countries[randomNumber2];
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>quote gen</title>
</head>
<body>
<h1>simple quote generator</h1>
<div id="quoteDisplay">
</div>
<button onclick="newQuote()"> New Quote</button>
<div id="countryDisplay">
</div>
<button onclick="newCountry()"> New Country</button>
</body>
</html>
答案 1 :(得分:0)
检查您的第二个功能。您使用的是冒号而不是分号。
function newCountry() {
var randomNumber2 = Math.floor(Math.random() * (countries.length)): <--
document.getElementById('countryDisplay').innerHTML =countries[randomNumber2];
};
已更正:
function newCountry() {
var randomNumber2 = Math.floor(Math.random() * (countries.length));
document.getElementById('countryDisplay').innerHTML =countries[randomNumber2];
};