如何使一个按钮可重用? [英] How do I make a button reusable?

查看:104
本文介绍了如何使一个按钮可重用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的编程世界。我有HTML和CSS的一个坚固的知识,最近回升的JavaScript。我工作的一系列文字发电机作为学校项目的;我的目标是能够,在网站上,点击一个按钮,使计算机每次吐出随机文本单击该按钮。然而,尽管我对HTML和JavaScript把握好,我对如何将二者结合起来的知识给网页的功能几乎是不存在。

I am relatively new to the world of programming. I have a sturdy knowledge of HTML and CSS, and recently picked up JavaScript. I am working on a series of text generators as a school project; my goal is to be able to, on the website, click a button and have the computer spit out random text each time you click the button. However, while I have a good grasp on HTML and JavaScript, my knowledge on how to combine the two to give a webpage functionality is virtually nonexistent.

我创建了一个莎士比亚侮辱生成器使用JavaScript,这是功能性的,我想通了如何将按钮添加到页面,以便当您单击按钮,它打印一个随机生成的侮辱页:

I created a "Shakespearean Insult Generator" using JavaScript, which is functional, and I figured out how to add a button to a page so that when you click the button, it prints a randomly generated insult to the page:

<script>
var adjective1 = ["artless", "bawdy", "beslubbering"...
var adjective2 = ["base-court", "bat-fowling", "beef-witted"...
var noun = ["apple-john", "baggage", "barnacle"...

var insult = "Thou art a " + adjective1[Math.floor(Math.random() * 60)] + ", " + adjective2[Math.floor(Math.random() * 60)] + ", " + noun[Math.floor(Math.random() * 60)] + "!";

var genInsult = function() {
x=document.getElementById("replace");  
x.innerHTML=insult;    
};
</script>

<p id= "replace">Your insult will appear here!</p>
<button type="button" onclick="genInsult()">Generate Insult</button>

然而,一旦你一次$ $ p PSS的按钮,你不能preSS一遍,除非你刷新页面生成另一个侮辱。

However, once you press the button once, you cannot press it again to generate another insult unless you refresh the page.

所以我的问题是:我如何通过使用JavaScript使得该按钮可重复使用

So my question is: how can I make this button reusable by using JavaScript?

我试图寻找一个答案,我的问题,但问题是,我很新的JavaScript来我经常听不太懂别人的问题和他们的答案。此外,大量的回应参考jQuery的,我不知道的语言。如果任何人有JavaScript的领域内的解决方案,我将非常感谢!

I've tried searching for an answer to my question, but the problem is that I'm so new to JavaScript that I often have trouble understanding other people's questions and the answers to them. Also, a lot of responses reference jQuery, a language I do not know. If anyone has a solution within the realm of JavaScript, I would be extremely grateful!

我可能不知道很多赫克了,但我非常渴望学习!

I might not know a heck of a lot now, but I am very eager to learn!

推荐答案

移动此:

var insult = "Thou art a " + adjective1[Math.floor(Math.random() * 60)] + ", " + adjective2[Math.floor(Math.random() * 60)] + ", " + noun[Math.floor(Math.random() * 60)] + "!";

genInsult()函数中,你应该是不错的。现在它是外,使它只生成一次。

within your genInsult() function and you should be good. Right now it's outside so it will only generate once.

这篇关于如何使一个按钮可重用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆