javascript - How to get the originalEvent without jQuery on iPhone - Stack Overflow

For some reason, I can't use jQuery.This is my code :document.addEventListener("touchstart&qu

For some reason, I can't use jQuery.

This is my code :

document.addEventListener("touchstart", function(e) {
    e.preventDefault();

    var orig = e.originalEvent;

    var x = orig.changedTouches[0].pageX;
    var y = orig.changedTouches[0].pageY;

    //id("#draggable").css({top: y, left: x});
    id("draggable").style.left = x;
    id("draggable").style.top = y;

});

Using jQuery, you can get the originalEvent , but if you don't use it, how to get it?

Thanks

For some reason, I can't use jQuery.

This is my code :

document.addEventListener("touchstart", function(e) {
    e.preventDefault();

    var orig = e.originalEvent;

    var x = orig.changedTouches[0].pageX;
    var y = orig.changedTouches[0].pageY;

    //id("#draggable").css({top: y, left: x});
    id("draggable").style.left = x;
    id("draggable").style.top = y;

});

Using jQuery, you can get the originalEvent , but if you don't use it, how to get it?

Thanks

Share Improve this question edited May 9, 2016 at 5:54 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Oct 4, 2010 at 6:04 zjm1126zjm1126 35.7k53 gold badges125 silver badges167 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 11

jQuery's event object that it passes to event listener function is jQuery's own creation. Its originalEvent property is the actual Event object created by the browser, so if you attach an event listener without jQuery, the event object that is passed to your listener is exactly the same as the originalEvent property of a jQuery event object. Therefore in your example, e is precisely what e.originalEvent would point to if you'd used jQuery.

I've e with a simple solution for your question. I added a global variable "hovered" and mouseover, mouseout events to the button. When the mouse hovers the button then variable "hovered" is set to true and if the mouse hovers off the button it is set to false. This mechanism allows you to determine whether the mouse is on the button while the onchange event fires. So if the mouse is hovering on the button while the onchange event is triggered then we know that the user clicked the button and that triggered onchange.

Of course this is a quick and dirty solution just to give you an idea. It can be done better. Hope it helps:)

var hovered = false;

$('#button').mouseover(function(){
    hovered = true;   
}).mouseleave(function(){
    hovered = false;
});

$(function(){
    $('#button').bind("click",function(){
          alert("button click");
      });
$('#textbox').bind("change",function(){

    alert("text changed");
    if (hovered)
        $('#button').trigger('click');                
});

});​
var x = e.pageX;

If you have Safari on your desktop, open it in iPhone mode
Develop-> User Agent-> Mobile Safari...
And put a breakpoint inside your function, the you can inspect the e object and all its properties.

I think you're using changedTouches, which is a list of Touches for every point of contact which contributed to the event. Multi-touch is an example of when this would happen. For touchstart you might want to just check touches, a list of Touches for every point of contact currently touching the surface.

var origTarget = e.originalEvent.touches[0].target
var x = e.originalEvent.touches[0].pageX
var y = e.originalEvent.touches[0].pageY

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743724549a4496394.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信