每次点击都改变文字 [英] change text to everytime I click

查看:120
本文介绍了每次点击都改变文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想这样做,而且我对JavaScript还比较陌生: 第一次按钮是推送时,文本应改为您按下按钮。 (不包括引号)。 第二次按下按钮时,文本应该改为您再次按下按钮。 (不包括引号)。

  • 按下按钮的第三至第五次,文本应该改为您按了[n]次按钮。 (不含引号)。如果按钮被按下六次或更多次,则文本应该被替换为已被按下的次数。

  • 停止按下按钮。 (不含引号)。


    这就是我现在所拥有的:

      function go(){
    // alert(alert!);
    var paragraph = document.getElementById(output);
    paragraph.innerHTML =您按下了按钮;


    函数go2(){
    var paragraph2 = document.getElementById(output)
    paragraph2.innerHTML =您再次按下按钮 ;
    }

    HTML是这里: https://gyazo.com/8f24747521b539e2a68058716126279f



    任何帮助:(请某人??

    解决方案

    您可以尝试如下所示:

    JS

      var clicks = 0; 
    函数onClick(){
    clicks + = 1;
    var message = ;
    if(clicks == 1)
    {message =您按下了按钮。;}
    else if(clicks == 2)
    {message =You push ;}
    else if(clicks> = 6)//点击6次以上
    {message =停止按下按钮;}
    else
    {message =您按下了按钮+ clicks +times。;}
    document.getElementById(message)。innerHTML = message;

    };

    html:

     < button type =buttonid =buttonclickon点击=onClick()>点击我< /按钮> 
    < div id =message>< / div>

    例子: http://codepen.io/anon/pen/MaEExW


    So I want to do this, and I'm fairly new to JavaScript:

    1. The first time the button is pushed, the text should be changed to "You pushed the button." (no quotes).

    2. The second time the button is pushed, the text should be changed to "You pushed the button (again)." (no quotes).

    3. The third through fifth times the button is pressed, the text should be changed to "You pushed the button [n] times." (no quotes). [n] should be replaced by the number of times the button has been pressed.

    4. If then button is pressed six or more times, the text should be replaced with "Stop pushing the button." (no quotes).

    This is what I currently have:

    function go() {
        // alert("alert!");
        var paragraph = document.getElementById("output");
        paragraph.innerHTML = "You pushed the button";
    }
    
    function go2() {
        var paragraph2 = document.getElementById("output")
        paragraph2.innerHTML = "You pushed the button (again)";
    }
    

    THE HTML IS HERE : https://gyazo.com/8f24747521b539e2a68058716126279f

    any helps :( please someone??

    解决方案

    you can try something like this:

    JS

        var clicks = 0;
     function onClick() {
       clicks += 1;
       var message = "";
       if(clicks==1)
         { message = "You pushed the button.";}
       else if(clicks==2)
       {message ="You pushed the button (again).";}
        else if(clicks >= 6) //for 6 clicks and above
       {message ="Stop pushing the button.";}
       else
         {message = "You pushed the button " + clicks + " times.";}
       document.getElementById("message").innerHTML = message;
    
     };
    

    html:

         <button type="button" id="buttonclick" onClick="onClick()">Click me</button>
    <div id="message"></div>
    

    example: http://codepen.io/anon/pen/MaEExW

    这篇关于每次点击都改变文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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