简体   繁体   English

使可点击的按钮更改文本

[英]Making a clickable button change text

Source Code link: http://hexmerchant.github.io/ 源代码链接: http : //hexmerchant.github.io/

I'm looking to make this button display different text each time I click it. 我希望每次单击该按钮时都显示不同的文本。 i'm using html, css, and js. 我正在使用html,css和js。

<button onclick="exploreFunction()">Explore</button>

That's the button. 那是按钮。 Here is the first function. 这是第一个功能。

function exploreFunction() {
    var person = prompt("What is your name", "");

    if (person != null) {
        document.getElementById("story1").innerHTML =
        "-You wake up lying on the ground. You feel a large deposit of energy inside you.";
    }
}

What do I need to do to accomplish this? 我需要怎么做才能做到这一点?

This could help multiple people out. 这可以帮助多个人。 As I was searching around for an answer here I realized that each answer was so specific that I could not find a match for my topic. 当我在这里寻找答案时,我意识到每个答案都是如此具体,以至于找不到与我的主题相匹配的内容。

I'm very new to all this and trying to teach myself... got this far : ) 我对这一切都很陌生,并试图自学……走了这么远:)

Just add an ID to the button, like - 只需在按钮上添加一个ID,例如-

<button id="myButton" onclick="exploreFunction()">Explore</button>

And then you can add to exploreFunction() a very similar command to what you did to change the text - 然后,您可以向explorerFunction()添加与更改文本非常相似的命令-

document.getElementById("myButton").value = "New display text";

If you want the text to change every time, you can insert the name from the prompt if you like. 如果您希望每次更改文本,可以根据需要在提示符下插入名称。

 function exploreFunction() { var person = prompt("What is your name", ""); if (person != null) { document.getElementById("story1").innerHTML = person + ", you wake up lying on the ground. You feel a large deposit of energy inside you."; } } 
 <button onclick="exploreFunction()">Explore</button> <div id="story1"></div> 

If you want the text in the story to change on every button click and the prompt only to be displayed the first time the button is clicked, you could use following approach: 如果希望故事中的文本在每次单击按钮时都发生变化,并且仅在第一次单击按钮时才显示提示,则可以使用以下方法:

var current = 0;
var person;
var story = ["-You wake up lying on the ground. You feel a large deposit of 
    energy inside you.", "-You go to the right.", "-The path is blocked."];

function exploreFunction() {
  if (current == 0) {
    person = prompt("What is your name", "");
  }
  if (person != null) {
    document.getElementById("story1").innerHTML = story[current];
    current += 1;
  }
}

and add elements to the story. 并为故事添加元素。 Fiddle 小提琴
To avoid any errors when the last element of the story is displayed, you can either remove / disable the button or adjust the function to display the text eg like this: 为避免在显示故事的最后一个元素时出现任何错误,您可以删除/禁用按钮或调整功能以显示文本,例如:

if (person != null && current < story.length) {
...

Adjusted Fiddle for that. 为此调整了小提琴

This function isn't specific but generic. 此功能不是特定功能而是通用功能。 With the code below you can assign this function to all buttons if you like. 如果需要,可以使用下面的代码将此功能分配给所有按钮。 I'd advice you to store your different texts in an array 我建议您将不同的文本存储在数组中

example

<button onclick="exploreFunction(this, functionNext)" >Explore</button>
function exploreFunction(button, functionToContinue) {
    var person = prompt("What is your name", "");

    if (person != null) {
        document.getElementById("story1").innerHTML =
        "-You wake up lying on the ground. You feel a large deposit of energy inside you.";
        button.onclick = functionToContinue;
    }


}


function functionNext()
{
        //Example code  
        document.getElementById("story1").innerHTML =
        "-The tooth fairy really came through and you feel enchanted."; 
        //Other code come here          

        this.onclick = [new function]; //add your own function name here do not add () because it will get executed when you do, and you want it executed when the user clicks the button.
}

What does the code above do: 上面的代码做什么:

  1. Adds the exploreFunction to the button (refer to itself by adding this to the arguments). exploreFunction添加到按钮(通过将其添加到参数来引用自身)。
  2. Supply the next function as argument 提供下一个函数作为参数
  3. function exploreFunction get executed , name is prompted and innerHTML of story1 is updated. 函数exploreFunction得到执行,提示名称,并更新story1的innerHTML。
  4. button refers to the input element. 按钮是指输入元素。 Assign a new onclick handler functionNext 分配一个新的onclick处理functionNext
  5. Next time the user clicks on the button functionNext gets executed. 下次用户单击按钮functionNext时,Next将被执行。
  6. functionNext assigns another onclick handler (to be made by you). functionNext分配另一个onclick处理程序(由您执行)。

Are you catching the drift? 您正在赶上漂流吗?

Since you have the button object in exploreFunction and also in the subsequent functions the this variable refers to the button object, you can adjust other properties of this button. 由于在exploreFunction和后续函数中都有按钮对象,因此this变量引用按钮对象,因此可以调整此按钮的其他属性。 Such as the value. 如值。

example in the functionNext : functionNext Next中的示例:

 this.value = "Click me to advance";

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

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