Javascript 禁用img(用作按钮)
图像是:Javascript 禁用img(用作按钮),javascript,html,Javascript,Html,图像是: <img id="lawnButton" src="images/mowing.png" width="15%" height="45%" alt="mowLawn" border="0" onclick="cutLawn()"> 有没有什么事情是我做得不对的?执行代码后,图像仍然作为按钮工作。图像标记没有该属性。尝试删除该事件 var lawn = document.getElementById("lawnButton"); lawn.onclick = functi
<img id="lawnButton" src="images/mowing.png" width="15%" height="45%" alt="mowLawn" border="0" onclick="cutLawn()">
有没有什么事情是我做得不对的?执行代码后,图像仍然作为按钮工作。图像标记没有该属性。尝试删除该事件
var lawn = document.getElementById("lawnButton");
lawn.onclick = function() {
//do code
lawn.onclick = null;
}
//.. code to re-enable the trigger
if (some condition) lawn.onclick = cutLawn;
由于图像没有禁用的属性,您可能需要为其设置一些标志,以便在
cutLawn
中使用
你可以用一节课。例如,要禁用该按钮,请执行以下操作:
button = document.getElementById("lawnButton")
button.className = button.className+ " disabled"
然后在cutLawn
中:
cutLawn = function(){
button = document.getElementById("lawnButton")
if button.className.match(/disabled/){
return false;
}
// ...
}
我认为图像没有禁用的属性。你知道有没有类似的东西可以使用吗?你也可以在img周围使用,然后你可以将按钮设置为禁用。效果非常好!非常感谢。
cutLawn = function(){
button = document.getElementById("lawnButton")
if button.className.match(/disabled/){
return false;
}
// ...
}