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

查看:22
本文介绍了获取所有带有 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天全站免登陆