我是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>
这是适合您的工作代码。您有很多syntax error
的和typos
。
您没有正确编写arrays
(引号)和(国家)内容,因此您的代码无法正常工作。同样,您也不需要关闭;
其不需要的功能。
您可以在此处阅读有关JS数组的更多信息
两个按钮都可以使用,并显示结果 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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句