how to disable or enable all onClick for images on a page

前端 未结 5 1555
不知归路
不知归路 2021-01-17 17:40

When the user clicks on an image, I want the onClicks on all other images to be disabled until my function has finished.

I currently have this code that disables the

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-17 18:24

    Your solution doesn't work because you removed your onClick with onClick = false. After that you need to create the onClick event handler again.

    This is probably your way of adding onclick events, I changed it so it should work.

    
    
    

    Try adding a function to your onclick as above.

    Your onclick function:

    var when_i_click = function(){
        alert('image clicked!');
    }
    

    This is how you disable your onclicks (your method)

    var eles = document.getElementsByTagName('img');
    for (var i=0; i < eles.length; i++)
       eles[i].onclick = null;
    

    This is how you re-enable them (re-attach function to onClick )

    var eles = document.getElementsByTagName('img');
    for (var i=0; i < eles.length; i++)
       eles[i].onclick = when_i_click;
    

    This is a Jquery solution jquery:

    Try using unobstructive javascript, by not adding onclick event handlers in the DOM.

    
    

    When your function is finished just set function_is_finished to true

提交回复
热议问题