How to hook into the page wide click event?

孤街醉人 提交于 2019-12-02 03:15:51

问题


Just like the question states. I want to fire off an event that calls a method everytime the user clicks on the web page.

How do I do that without use of jQuery?


回答1:


Without using jQuery, I think you could do it like this:

if (document.addEventListener) {
    document.addEventListener('click',
        function (event) {
            // handle event here
        },
        false
    );
} else if (document.attachEvent) {
    document.attachEvent('onclick',
        function (event) {
            // handle event here
        }
    );
}



回答2:


Here's one way to do it..

if (window.addEventListener)
{    
    window.addEventListener('click', function (evt)
    {
        //do something
    }, false);
} 
else if(window.attachEvent)
{
    window.attachEvent('onclick', function (evt)
    {
        // do something (for IE)
    });
}



回答3:


$(document).click(function(){});



回答4:


document.onclick = function() { alert("hello"); };

note that this will only allow for one such function though.



来源:https://stackoverflow.com/questions/5412234/how-to-hook-into-the-page-wide-click-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!