Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 禁用img(用作按钮)_Javascript_Html - Fatal编程技术网

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;
    }
    // ...
}