【问题标题】:how to make a button usable more than once?如何使按钮多次可用?
【发布时间】:2014-06-30 12:09:16
【问题描述】:

我正在制作一个程序,当单击按钮时,它会使文本从数组中随机出现(我已经完成了这部分)。我需要帮助的是使按钮能够被反复按下并每次都显示其他内容。

input type="button" value="Maths" 
onClick = "document.getElementById('4').style.display='block';">
div id="4" style="display:none;"

script language="Javascript"
sent = new Array (6);
sent [0] = "Don't cry because it's over. Smile because it happened. -Dr.Seuss";
sent [1] = "So many books, so little time. -Frank Zappa";
sent [2] = "In three words I can sum up everything I've learned about life: it goes on. -Robert Frost";
sent [3] = "If you tell the truth, you don't have to remember anything. -Mark Twain";
sent [4] = "To live is the rarest thing in the world. Most people exist, that is all. -Oscar Wilde";
sent [5] = "To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment. - Ralph Waldo Emerson";
x=Math.floor(Math.random()*6);  

    document.write(sent[x])
/script/div

【问题讨论】:

  • 你为什么不使用实际的 HTML,而使用 document.write()
  • 我不明白你的意思这是我老师强硬的。我确实使用了 document.write()
  • 你有什么问题?你遇到了什么问题?
  • 我的意思是:input type="button" value="Maths" 应该(可能)是格式良好的 HTML:<input type="button" value="Maths" />,是的,你使用的是 document.write(),但你不应该是(参见:stackoverflow.com/questions/802854/… )。
  • 这不是一个让人们只为你做功课的网站。做一些研究(这方面有大量的资源),然后返回您遇到的特定问题。

标签: javascript html button


【解决方案1】:

从数组中删除显示的元素

sent.splice(x,1);

【讨论】:

  • 这会从“sent”数组中删除索引为“x”的元素。第二个参数是要从索引中删除的元素数(第一个参数)。 check this
【解决方案2】:

你应该把它放在一个循环中。这将使代码持久化。

while (condition) {
code block to be executed
}

【讨论】:

  • 把整个东西放在花括号里?
  • 是的。将您的代码放入 while 循环并创建一个有效条件。
  • 但是每次点击时会重复什么条件?对不起,我对 javascript 编码真的很陌生
  • 你放在while循环中的代码会被重复。
  • 我试着把 while(x==1,x
猜你喜欢
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多