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 的元素?
基础教程推荐
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
