How to trigger a click event on disabled elements

后端 未结 5 932
难免孤独
难免孤独 2021-01-11 23:18

I have a disabled button, which is enabled after checking \"I accept terms and conditions\" checkbox. The problem is that I wanted to trigger an alert, if a user cli

5条回答
  •  自闭症患者
    2021-01-12 00:00

    You can write a function that adds listeners to the mousedown and mouseup events, and if the targets match your Node (i.e. the mousedown and following mouseup were on your element), then it invokes another function

    function listenFullClick(elm, fn) {
        var last;
        document.addEventListener('mousedown', function (e) {
            last = e.target === elm;
        });
        document.addEventListener('mouseup', function (e) {
            if (e.target === elm && last) fn();
        });
    };
    
    listenFullClick(
        document.getElementById('foo'), // node to look for
        function () {alert('bar');}     // function to invoke
    );
    

    DEMO

提交回复
热议问题