get all elements with onclick in them?

后端 未结 6 1327
灰色年华
灰色年华 2020-12-19 15:32

I am looking for a way to list all elements that have onclick event on a page.

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 16:20

    Edited to answer further questions...


    I'm not sure exactly what you're asking, but I suspect you're looking for a way to see if there has been code attached to an element's onclick event? If so, try this:

    var allElements = document.getElementsByTagName('*');
    for ( var i = 0; i

    If you'd like some information about the actual function attached to the onclick event, you'll need to make use of Firebug or something similar to output the function and examine it as it will have been possibly generated at runtime (ie the function declaration might not just be sitting on the page where it could theoretically be accessed easily). Try using the code provided above, but instead of

    console.log( allElements[i] );

    do

    console.log( allElements[i].onclick );

    Now, in firebug, you can mouse over the functions it prints and see their code.

提交回复
热议问题