Catching the Click on DOM/HTML/BODY event on the iPad

前端 未结 7 1028
梦毁少年i
梦毁少年i 2021-02-04 05:55

I\'m using jQuery to detect a click on the DOM - or let\'s every click.

$(document).click(function(){
   alert(\"Click :-)\");
});

This works p

相关标签:
7条回答
  • 2021-02-04 06:19
    $('html').click(function(){
       alert("Click :-)");
    });
    

    This works for me, I tested it now.

    Even works with no content on page, wherever you click on the page.

    0 讨论(0)
  • 2021-02-04 06:24

    You can attach the click listener to the main wrapper element (say div that encloses all the components in your page).

    0 讨论(0)
  • 2021-02-04 06:25

    You may use the touchstart event instead.

    0 讨论(0)
  • 2021-02-04 06:28

    These answers got me started on the right direction (first google for "jquery document.on ipad"), so thanks for that, but borrowing from this answer I simplified it to something like this:

    $(document).on("click touchstart", function (ev) {
        ...
    });
    

    This would obviously produce undesired results if a platform (like Android or Windows Phone or something) supported both click and touchstart in the event bubble, so if anyone knows that let me know (and I'll fix my code and delete this answer! :)

    0 讨论(0)
  • 2021-02-04 06:32

    I have used this:

    jQuery(document).on('touchstart',function(event){
        //your code here
             });
    

    -also instead of "document" you can bind to any DOM object.

    and this(from another forum answer):

    var body = document.getElementsByTagName('body')[0];
    
     if ("ontouchstart" in window) {
    
            body.ontouchstart = function(){
       //your code here
             };     
                     };
    

    -again, you don't have to use 'body' you can assign a variable to an object by class this way:

    var dd = document.getElementsByClassName('infoAction')[0]; 
    
    0 讨论(0)
  • 2021-02-04 06:40
    <body><div onclick="void(0)">
    ... your page tags ...
    </div></body>
    

    There is a minor difference with others browsers behaviour: the document object will reveive click events only for tags located inside the "... your page tags ..." section.

    In other words, suppose your html and body tags have a yellow background color, and their child tags have a red background color. The document object will receive clicks on the red areas only. This is usually not a serious drawback.

    Tested on an iPhone 3 only

    0 讨论(0)
提交回复
热议问题