get all elements with onclick in them?(获取所有带有 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 的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取所有带有 onclick 的元素?


- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06