How to get Events associated with DOM elements?

后端 未结 4 2229
遥遥无期
遥遥无期 2021-02-14 02:27

I have an HTML document.
It is possible to get the events associated with every Element in a particular FORM element in the document using javascrip

4条回答
  •  感情败类
    2021-02-14 03:23

    As Felix said in his comment, there are several ways to register an event on an object. So how you can get the events attached to an object and serialize them somehow to your xml depends on how they are registered. I will list some thoughts of how you can get them for serialisation.

    1 Handlers registered inline

    1.1 Completely inline code:

    
    

    When the code is completely inline, you can just get the attribute in your xml and save it.

    2.1 Inline function call

    In this case you would have to export the declaration of the function. In JavaScript Functions are objects itsself so you could actually get the declaration text of your function by invoking myFunc.toString(). The hard part on this would be to figure out, if this is a function call where the declaration hast to be exported or not.

    2 Handlers registered through attributes

    If you have added all your element Handlers through i.e. :

    function myFunc(num){
      alert("Your number is: " + num);
    }
    
    document.getElementById('myElement').onclick = myFunc;
    

    you could just iterate your form elements like you already do and get the onlick, onmouseover, onblur, on.... properties all one by one and save them to xml. Also in this case the content of this propertys will be Function Objects as well so to save their actual content you have to do .toString() on the Function object.

    In addition there are some other ways to register Event handlers depending on the different Browsers. So if you definetly know how your events are registered, you can actually serialize them. If you don't that's going to be very difficult.

    I hope that helps to get you a bit further.

提交回复
热议问题