我希望通过点击操作来禁用第五次点击的按钮,但是我无法在 这是javascript ...
var currentTurn = 1;
function taketurn(){
currentTurn++;
document.getElementById("turn").innerHTML = currentTurn;
threeturns(currentTurn);
}
function threeturns(now) {
if(now > 3){
document.getElementById("livedead").innerHTML = "disabled";
}
}
然后html
<div class="container">
<h1>turn <span id="turn">1</span></h1>
<button onclick="taketurn()" type="button" class="btn btn-lg btn-info" <span id="livedead"></span> /> next</button>
<button onclick="taketurn()" type="button" class="btn btn-lg btn-info" disabled> next</button>
</div>
答案 0 :(得分:1)
您的代码有些变化。需要对HTML进行一些更正,以使livedead
跨度未在开始<button>
标记内定义。
此外,通过在按钮disabled
对象上设置Node
字段来禁用javascript按钮(即,不通过将innerHTML设置为Disabled来完成)。
我建议对您的代码进行一些更改-关键之一是使用MouseEvent
对象。
在您的情况下,这将简化禁用用户单击的按钮的操作。为了说明MouseEvent
对象的用法,请考虑以下代码:
var currentTurn = 1;
// add a parameter to function to give us access to the event
// for this click interaction
function taketurn(event){
// if click count is 5 or more, then disable this button
if(currentTurn >= 5) {
// we access the button via the event parameter that we
// passed in. The currentTarget represents the button, so
// we can set disabled on the button to disable it.
event.currentTarget.disabled = 'disabled'
}
currentTurn ++;
document.getElementById("turn").innerHTML = currentTurn;
}
<div class="container">
<h1>turn <span id="turn">1</span></h1>
<!-- pass event defined for the click interaction, to taketurn() -->
<button onclick="taketurn(event)" type="button" class="btn btn-lg btn-info" id="livedead"> next</button>
<button onclick="taketurn()" type="button" class="btn btn-lg btn-info" disabled> next</button>
</div>