开发者

How to hook into the page wide click event?

开发者 https://www.devze.com 2023-02-19 03:01 出处:网络
Just like the question states.I want to fire off an event that calls a method everytime the user clicks o开发者_开发百科n the web page.

Just like the question states. I want to fire off an event that calls a method everytime the user clicks o开发者_开发百科n the web page.

How do I do that without use of jQuery?


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
        }
    );
}


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)
    });
}


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


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消