Why is the event object different coming from jquery bind vs. addEventListener

前端 未结 1 1185
猫巷女王i
猫巷女王i 2020-12-31 06:00

Why is it when I use the jQuery bind the event object I get back is different from the event object I get back using addEventListener?

The event object resulting fro

1条回答
  •  别那么骄傲
    2020-12-31 06:41

    That's because jQuery uses its own Event model.

    jQuery simply copies and normalizes some properties from the original event, to the event object that you get as the first argument of the handler.

    The copied properties are based on the DOM Level 3 Events Spec.

    To get the original event object, you can:

    $(document).ready (function () {
        $("#test").bind("touchmove", function (event) {
            var e = event.originalEvent;
            console.log(e.targetTouches[0].pageX);
        });
    });
    

    The originalEvent property is accessible and it will work, but is not documented, you can see how it's set behind the scenes in the jQuery.Event constructor.

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