通过它已被点击所花费的时间更改按钮文本 [英] Change text of button by the amount of times it has been clicked

查看:128
本文介绍了通过它已被点击所花费的时间更改按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于实践的检验,问我找到一种方法,使一个按钮显示段落中的文本。每次它是pressed,文本必须改变。

For a practice test I am asked to find a way to make a button reveal text in a paragraph. Each time it is pressed, the text must change.

第一次pressed按钮应该说你pressed按钮
第二个你$ P $再次pssed它
第三至第五次你pressed按钮(一个数字从3到5)次
和第六及以后,应该说停!

For the first time pressed the button should say "you pressed the button" second "you pressed it again third to fifth time "you pressed the button (a number from 3 to 5) times" and sixth and onward it should say "stop!"

编辑编辑修改

这是整个原始的HTML,我不知道,如果它是需要的,但也许是HTML可能是与JavaScript的codeS你们给了我不为我工作。

This is the entire Original HTML, i am not sure if it is needed, but maybe the html could have something to do with the javascript codes you all have given me to not work for me.

HTML

<!DOCTYPE html>
<html>
<head>
<script src="q2.js" type="text/javascript"></script>
</head>
<body>

<button type="button" onclick="go()">ClickMe</button>

<p id="output">
</p>

</body>
</html>

的JavaScript

function go() {
    var out = document.getElementById("output");
    var x = "hi there";
    out.innerHTML = x;
}

我应该怎么做,使这项工作?

What should I do to make this work?

推荐答案

使用开关语句,以避免嵌套的如果 ...语句

use a switch statement to avoid nested if statements...

var testCount = 0;
var out = document.getElementById("output");

function go(){
  testCount++;
  switch (testCount) {
    case 1:
      out.innerHTML = 'you pressed the button';
      break;

    case 2:
      out.innerHTML = 'you pressed it again';
      break;

    case 3:
    case 4:
    case 5:
      out.innerHTML = 'you pressed the button ' + testCount + ' times';
      break;

    default:
      out.innerHTML = 'stop!!';
      break;
  }
}

这篇关于通过它已被点击所花费的时间更改按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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