Проблемы с несколькими кнопками JS - PullRequest
1 голос
/ 09 июля 2020

Я новичок в Javascript, и у меня проблемы с несколькими кнопками. Когда у меня есть только первая кнопка, она работает нормально, но как только я добавляю javascript для второй кнопки, ни одна из кнопок не работает! Я не думаю, что есть проблема с html, но я включил его на всякий случай.

Javascript section:

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>

Ответы [ 2 ]

0 голосов
/ 09 июля 2020

Обратите внимание на вашу вторую функцию. У вас двоеточие вместо точки с запятой.

 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];
    };
0 голосов
/ 09 июля 2020

Вот вам рабочий код. У вас много syntax error s и много 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...