开发者

On click function without selector / anything as selector?

Is it possible with jQuery to have an onclick function without any selector? So instead of this:

$('#div').click(function() {

You would have this:

$(anything).click(function() {

Ive tried using html and body as the selector. This works fine except on iPhones, so is there a standard way of s开发者_JAVA百科aying 'anything' as a selector?


Try this

$('*').click(function() {});

or better delegate it back to the body

$('body').delegate('*', 'click', function() {});

as Lonesomeday Suggested as an alternative

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


I have no idea whether it will work on iPhones, but you should be able to avoid the universal selector by simply detecting the click on document.

$(document).click(function() {


Although you can bind the click event to every element using:

$('*').click(function(){...});

This will cause a lot of duplication of event handlers

Instead, you could use:

$(window).click(function(){});
-or-
$(document).click(function(){});

to just listen for a click anywhere.

Alternatively, if you must use element selectors, you could try:

$('body').delegate('*', 'click', function(){...});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜