How to get Events associated with DOM elements?

后端 未结 4 2227
遥遥无期
遥遥无期 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:01

    Though It looks weird to answer my own question but may be it will help some one :

    I did some research and found that: document.forms[i].elements[j].onclick will return

    function onclick()
    {
        login()
    }
    

    Here is the method which I was calling onClick of the button
    similarly we can check many others like:

    document.forms[i].elements[j].onblur
    document.forms[i].elements[j].onfocus etc. etc.



    Many thanks to all for taking interest in this question.

    0 讨论(0)
  • 2021-02-14 03:09

    There is a very good tool visual event that would display all the events associated with element visually.

    Add the following code at the end of each page that you want to monitor.

    This will make the page disable and highlight all the events with icons and also show what they are doing by taking the cursor on that icon.

    (function()
    {
        if(typeof VisualEvent!='undefined')
        {
            if(document.getElementById('Event_display'))
            {
                VisualEvent.fnClose();
            }else{
                VisualEvent.fnInit();
            }
        }else
        {
            var n=document.createElement('script');
            n.setAttribute('language','JavaScript');
            n.setAttribute('src','http://www.sprymedia.co.uk/design/event/media/js/event-loader.js');
            document.body.appendChild(n);
        }
    })();
    

    Please read visual event for detail.

    0 讨论(0)
  • 2021-02-14 03:17

    This probably isn't what you're looking for but may help you atleast see which part of the DOM you need to be looking at - you can get a plugin for firebug which shows any jQuery events bound to DOM elements called firequery, and I think firebug on its own can show attached events in normal JS.

    Considering that firebug is written in JS, there obviously must be a way to do it. Unfortunately I don't have time to go through the source myself (I'd like to :D ) but you can find the repo here: http://code.google.com/p/fbug/source/browse/branches/firebug1.8/content/firebug/

    Sorry I can't be of more help and good luck

    0 讨论(0)
  • 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:

    <INPUT  type="hidden"  name="message"   Value =""  onclick="alert('hello')"/>
    

    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.

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