Javascript - 点击它后更改按钮功能

时间:2017-07-07 08:48:28

标签: javascript html

我有一个按钮和3个功能。 点击它后改变每个功能 请帮助我。

4 个答案:

答案 0 :(得分:1)

i dont understand your question, i think you mean like this one button

尝试并使用document.getElementById



<button id="btn" type="button" onclick="document.getElementById('btn').innerHTML = 'Hey There'">Try it</button>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个,

this传递给函数。它将定位相同的单击按钮元素

&#13;
&#13;
function change(that){
that.value="changed"
that.innerHTML="changed" //for inner html change
console.log(that.outerHTML)
}
&#13;
<button id="btn" type="button" onclick="change()">click</button>
&#13;
&#13;
&#13;

行代码

&#13;
&#13;
    <button id="btn" type="button" onclick="this.innerHTML='changed'">click</button>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

<button type="button" onclick="a()">click</button>

的javascript

var a = (function() {
    var clicked = 0;
    return function() {
        clicked++;
        if (clicked == 1) f1(); // improve this part 
        if (clicked == 2) f2();
        if (clicked == 3) f3();
    }

})()

function f1() {
    alert(1)
}

function f2() {
    alert(2)
}

function f3() {
    alert(3)
}

demo

答案 3 :(得分:0)

function change(that){
      that.value="Changed"
      that.innerHTML="Changed"
}

<button id="btn" type="button" onclick="change(this)">click</button


      or


<button id="btn" type="button" onclick = "this.innerHTML = 'Changed'" > click </button>

<button id="btn" type="button" onclick = "document.getElementById('btn').innerHTML = 'Changed' "> Try it </button>