获取所有带有onclick的元素? [英] get all elements with onclick in them?

查看:112
本文介绍了获取所有带有onclick的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种列出页面上所有具有onclick事件的元素的方法.

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

推荐答案

经过编辑,可以回答更多问题...

Edited to answer further questions...

我不确定您要问的是什么,但是我怀疑您正在寻找一种方法来查看是否有代码附加到元素的onclick事件上?如果是这样,请尝试以下操作:

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<allElements.length; i++ ) {
    if ( allElements[i].className !== 'theClassNameYoureLookingFor' ) {
        continue;
    }
    if ( typeof allElements[i].onclick === 'function' ) {
        // do something with the element here
        console.log( allElements[i] );
    }
}

如果您需要有关附加到onclick事件上的实际函数的一些信息,则需要使用Firebug或类似的东西来输出该函数并检查该函数,因为它可能是在运行时生成的(即函数声明可能不仅仅位于理论上可以轻松访问的页面上).尝试使用上面提供的代码,而不要使用

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]);

console.log( allElements[i] );

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

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

现在,在Firebug中,您可以将鼠标悬停在其打印的功能上,并查看其代码.

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

这篇关于获取所有带有onclick的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆