多个JS按钮出现问题

杰克·曼宁

我是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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

验证提交按钮时出现问题

来自分类Dev

C#浏览按钮出现问题

来自分类Dev

铁路4:“删除”按钮出现问题

来自分类Dev

使用“上传”按钮将多个图像上传到页面时出现问题...

来自分类Dev

Python 3.8.5中的Tkinter:在函数中创建多个图像作为按钮时出现问题

来自分类Dev

使用“上传”按钮将多个图像上传到页面时出现问题...

来自分类Dev

输出多个值时出现问题

来自分类Dev

下载多个PDF时出现问题

来自分类Dev

使用多个列表时出现问题

来自分类Dev

多个开关出现问题,NullPointerException

来自分类Dev

多个子查询结构出现问题

来自分类Dev

使用Ember JS中的块组件的语义UI / jQuery下拉按钮出现问题

来自分类Dev

使用Ember JS中的块组件的语义UI / jQuery下拉按钮出现问题

来自分类Dev

Angular.js-$资源即服务,多个URL出现问题

来自分类Dev

Office JS:在多个Excel实例中执行外接程序时出现问题

来自分类Dev

React.js:更新多个组件共享的上下文API时出现问题

来自分类Dev

带有Express异步的Node.js API:多个请求出现问题

来自分类Dev

JS函数执行出现问题

来自分类Dev

创建js日历时出现问题

来自分类Dev

Typeahead.js出现问题

来自分类Dev

使图像成为提交按钮时出现问题

来自分类Dev

使用Angular Dart中的“单选”按钮时出现问题

来自分类Dev

如果if-else语句导致按钮出现问题

来自分类Dev

在Django中创建删除按钮时出现问题

来自分类Dev

使用jQuery动态创建按钮时出现问题

来自分类Dev

尝试在kivy上制作圆形按钮时出现问题

来自分类Dev

按下某些按钮更改标签时出现问题

来自分类Dev

使用按钮动画加载场景异步时出现问题

来自分类Dev

多次使用单选按钮使渲染组件出现问题

Related 相关文章

  1. 1

    验证提交按钮时出现问题

  2. 2

    C#浏览按钮出现问题

  3. 3

    铁路4:“删除”按钮出现问题

  4. 4

    使用“上传”按钮将多个图像上传到页面时出现问题...

  5. 5

    Python 3.8.5中的Tkinter:在函数中创建多个图像作为按钮时出现问题

  6. 6

    使用“上传”按钮将多个图像上传到页面时出现问题...

  7. 7

    输出多个值时出现问题

  8. 8

    下载多个PDF时出现问题

  9. 9

    使用多个列表时出现问题

  10. 10

    多个开关出现问题,NullPointerException

  11. 11

    多个子查询结构出现问题

  12. 12

    使用Ember JS中的块组件的语义UI / jQuery下拉按钮出现问题

  13. 13

    使用Ember JS中的块组件的语义UI / jQuery下拉按钮出现问题

  14. 14

    Angular.js-$资源即服务,多个URL出现问题

  15. 15

    Office JS:在多个Excel实例中执行外接程序时出现问题

  16. 16

    React.js:更新多个组件共享的上下文API时出现问题

  17. 17

    带有Express异步的Node.js API:多个请求出现问题

  18. 18

    JS函数执行出现问题

  19. 19

    创建js日历时出现问题

  20. 20

    Typeahead.js出现问题

  21. 21

    使图像成为提交按钮时出现问题

  22. 22

    使用Angular Dart中的“单选”按钮时出现问题

  23. 23

    如果if-else语句导致按钮出现问题

  24. 24

    在Django中创建删除按钮时出现问题

  25. 25

    使用jQuery动态创建按钮时出现问题

  26. 26

    尝试在kivy上制作圆形按钮时出现问题

  27. 27

    按下某些按钮更改标签时出现问题

  28. 28

    使用按钮动画加载场景异步时出现问题

  29. 29

    多次使用单选按钮使渲染组件出现问题

热门标签

归档