【问题标题】:I need a way to make a multiple text output onclick button我需要一种方法来制作多个文本输出 onclick 按钮
【发布时间】:2019-09-06 17:47:44
【问题描述】:

对于我正在做的一个项目,我想要一个按钮,单击该按钮会导致不是按钮的文本在每次单击时都更改为新文本。例如第一次点击,“example 1”第二次,“example 2”另一个,“example 3”等...

我根据自己有限的编程知识尝试了一些东西,但没有任何效果。

【问题讨论】:

    标签: javascript html css button


    【解决方案1】:

    试试这个:

    var time = 0, output = document.getElementById("output");
    window.addEventListener("click", function(){
      time++;
      if (time == 1) output.textContent = "1";
      else if (time == 2) output.textContent = "2";
      else if (time == 3) output.textContent = "3";
      // repeat that pattern as many times as you want
    });
    <button id="button1">Click me!</button>
    <p id="output"></p>

    编辑:

    您应该查看switch statement,而不是使用 if/else 语句。

    【讨论】:

      【解决方案2】:

      这是基于上面提供的仍然准确的答案@VirxEC 的快速改进。我们不检查第一次、第二次或第三次,而是简单地显示时间变量。我还添加了用户在提问时描述的“示例”一词。

      var time = 0, output = document.getElementById("output");
      window.addEventListener("click", function(){
        time++;
        output.textContent = "example " + time;
      });
      <button id="button1">Click me!</button>
      <p id="output"></p>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-18
        • 2013-06-21
        相关资源
        最近更新 更多