Toggling click handlers in Javascript

后端 未结 6 832
庸人自扰
庸人自扰 2021-01-05 14:54

I have an HTML button to which I attach an event, using jQuery\'s bind(), like so:

$(\'#mybutton\').bind(\'click\', myFirstHandlerFunction);
         


        
6条回答
  •  轮回少年
    2021-01-05 15:35

    I was looking at this today, and realized there was still not a way to do this without a global variable listed. I came up with the following to add locations to an ESRI basemap, but it would work generally too:

    function addLocationClickHandler() {
        var addLocationClick = overviewMap.on('click', function (event) {
            addLocationClick.remove();
        })
        $('#locationAddButton').one('click', function (cancelEvent) {
            addLocationClick.remove();
            $('#locationAddButton').on('click', addLocationClickHandler)
        });
    }
    
    $('#locationAddButton').on('click', addLocationClickHandler)
    

    This should allow you to put something else in the section where you overwrite the click handler and not necessitate a global variable.

提交回复
热议问题