Get the element (node) which triggered an event

cronoklee

Is it possible to find the element which was listening for the event which triggered the current function?

In the code below, event.target is returning the lowest child node of #xScrollPane and event.currentTarget and event.fromElement are both null. Is there some other way to retrieve a DOM reference to #xScrollPane without hard coding it?

//convert vertical scroll wheel to horizontal scroll wheel:
function scrollWheelX(event) {
    if (event.deltaY == 0) return
    event.target.scroll(event.target.scrollLeft + event.deltaY * 5, event.target.scrollTop)
    event.preventDefault()
}
document.getElementById('xScrollPane').addEventListener('wheel', scrollWheelX)
radulfr

Edit: Inside the event handler, the this object refers to the element the event handler was added to.

In cases where this doesn't refer to the desired element or object, you can bind the element into the this object. Note that if you need to use removeEventListener, you have to pass the bound function to it instead of just the original function.

Example of binding even though in this case it wasn't necessary:

function scrollWheelX(event) {
    if(event.deltaY == 0) {
        return;
    }

    this.scroll(this.scrollLeft + event.deltaY * 5, this.scrollTop);
    event.preventDefault();
}

const scrollingElement = document.getElementById("scrollElement");
scrollingElement.addEventListener("wheel", scrollWheelX.bind(scrollingElement));

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Determine which element triggered a form submit event

分類Dev

Determine which element triggered a form submit event

分類Dev

Cancel window event if node event is also triggered

分類Dev

Get which Registry value changed when RegNotifyChangeKeyValue triggered in c#

分類Dev

How to get which element was clicked?

分類Dev

How to remove event listener of an element when I remove the corresponding element in the midst of the event being triggered in D3?

分類Dev

Get element from which onclick function is called

分類Dev

how to get random element of array which is not null?

分類Dev

How to get the child of a element with event.target

分類Dev

How to get the element that received the keydown event in TINYMCE

分類Dev

How to search an Event to get the name of an Element?

分類Dev

How to get a node which is before other?

分類Dev

How to get $scope or $element in an onblur event and run a function in an onblur event?

分類Dev

onKeyDown() event not triggered when in DialogFragment

分類Dev

How to get selector on which 'click' event has been bound?

分類Dev

Helm template: get node of first array element

分類Dev

Get an element of a String, which looks like XML and manipulate it in java

分類Dev

How to get element by attribute name which ends with defined word

分類Dev

How to get Element Properties in React Native on a Click Event

分類Dev

Vue.js + Element UI: Get "event.target" at change

分類Dev

why does my Action event is not being triggered?

分類Dev

Safari Only CSS Hover Event Not Triggered on Drag

分類Dev

Scroll event is triggered after Task Delay

分類Dev

jquery ajax data was send but no success event is triggered

分類Dev

Event binding with Static Ancestors leads to every click event being triggered

分類Dev

Function triggered when a new List element is added

分類Dev

Selenium for Python: Get text() of node that is shared with another element, via XPath

分類Dev

How to get dynamic node and element values in xml using vb?

分類Dev

Use terraform to set up a lambda function triggered by a scheduled event source

Related 関連記事

  1. 1

    Determine which element triggered a form submit event

  2. 2

    Determine which element triggered a form submit event

  3. 3

    Cancel window event if node event is also triggered

  4. 4

    Get which Registry value changed when RegNotifyChangeKeyValue triggered in c#

  5. 5

    How to get which element was clicked?

  6. 6

    How to remove event listener of an element when I remove the corresponding element in the midst of the event being triggered in D3?

  7. 7

    Get element from which onclick function is called

  8. 8

    how to get random element of array which is not null?

  9. 9

    How to get the child of a element with event.target

  10. 10

    How to get the element that received the keydown event in TINYMCE

  11. 11

    How to search an Event to get the name of an Element?

  12. 12

    How to get a node which is before other?

  13. 13

    How to get $scope or $element in an onblur event and run a function in an onblur event?

  14. 14

    onKeyDown() event not triggered when in DialogFragment

  15. 15

    How to get selector on which 'click' event has been bound?

  16. 16

    Helm template: get node of first array element

  17. 17

    Get an element of a String, which looks like XML and manipulate it in java

  18. 18

    How to get element by attribute name which ends with defined word

  19. 19

    How to get Element Properties in React Native on a Click Event

  20. 20

    Vue.js + Element UI: Get "event.target" at change

  21. 21

    why does my Action event is not being triggered?

  22. 22

    Safari Only CSS Hover Event Not Triggered on Drag

  23. 23

    Scroll event is triggered after Task Delay

  24. 24

    jquery ajax data was send but no success event is triggered

  25. 25

    Event binding with Static Ancestors leads to every click event being triggered

  26. 26

    Function triggered when a new List element is added

  27. 27

    Selenium for Python: Get text() of node that is shared with another element, via XPath

  28. 28

    How to get dynamic node and element values in xml using vb?

  29. 29

    Use terraform to set up a lambda function triggered by a scheduled event source

ホットタグ

アーカイブ