简体   繁体   English

单击我的按钮重新加载另一个句子

[英]Reload another sentence with an click on my button

I am working on this little project, but I am stuck.我正在做这个小项目,但我被卡住了。 I want my program to load a sentence or word every time I push on a button.我希望我的程序每次按下按钮时都加载一个句子或单词。 I got it working once, but when I push my button again it does not reload a new word.我让它工作了一次,但是当我再次按下按钮时,它不会重新加载一个新单词。

I tried to work with the page reloading, but that also does not work.我尝试重新加载页面,但这也不起作用。

Can anyone help me so that when I push the button it automatically generates a new random word from my list?任何人都可以帮助我,以便当我按下按钮时,它会自动从我的列表中生成一个新的随机单词吗? Keep in mind that I am very new to HTML, JavaScript and CSS.请记住,我对 HTML、JavaScript 和 CSS 非常陌生。

 function myFunction() { document.getElementById("demo").innerHTML = randomSong; } var song = Array("song1", "song2", "song3", "song4", "song5", "song6"); var randomSong = song[Math.floor(Math.random() * song.length)];
 <button onclick="myFunction();">Moet er een atje worden gedaan? </button> <p id="demo"></p>

<button onclick="myFunction();">Moet er een atje worden gedaan?
</button>
<p id="demo"></p>
<script>  function myFunction() {
    var song = Array("song1", "song2", "song3", "song4", "song5", "song6");
    var randomSong = song[Math.floor(Math.random() * song.length)];
    document.getElementById("demo").innerHTML = randomSong;
  }  
</script>

If your randomSong variable should get a new value everytime you click the button, then it should be inside the function myFunction which executes on every click如果您的 randomSong 变量在每次单击按钮时都应该获得一个新值,那么它应该在每次单击时执行的函数 myFunction 中

EDIT: Also, moving the variable declarations outside the function will be more good, as we don't have to re-create the variables every time.编辑:此外,将变量声明移到函数外会更好,因为我们不必每次都重新创建变量。

<script>
  var song = Array("song1", "song2", "song3", "song4", "song5", "song6");
  var randomSong;
  function myFunction() {

    randomSong = song[Math.floor(Math.random() * song.length)];
    document.getElementById("demo").innerHTML = randomSong;
  }  
</script>

EDIT2: Adding internal styles as per the request. EDIT2:根据请求添加内部样式。

<style>
  button { //selects the element with element name directly
    padding: 20px;
  }
  #demo { // selects the element with id
    font-size: 35px;
    color: blue;
  }
</style>

put this in your code把它放在你的代码中

<button onclick="myFunction();">Moet er een atje worden gedaan?</button>
<p id="demo"></p>
<script>
  var song = Array("song1", "song2", "song3", "song4", "song5", "song6");
  function myFunction() {
    document.getElementById("demo").innerHTML = song[Math.floor(Math.random() * song.length)];
  }  
</script>

Try this code.试试这个代码。

 function myFunction() { var randomSong = song[Math.floor(Math.random() * song.length)]; console.log(randomSong); document.getElementById("demo").innerHTML = randomSong; } var song = Array("song1", "song2", "song3", "song4", "song5", "song6");
 <button onclick="myFunction();">Moet er een atje worden gedaan? </button> <p id="demo"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM