如何使用javascript枚举文档中的所有html id? [英] How do I enumerate all of the html id's in a document with javascript?

查看:55
本文介绍了如何使用javascript枚举文档中的所有html id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用javascript为html文档中的每个对象查找每个id(或名称),以便可以将它们打印在页面底部.

I would like to be able to use javascript to find every id (or name) for every object in an html document so that they can be printed at the bottom of the page.

要更全面地了解我要完成的工作,请让我解释一下.我不时为诸如财产申请,租赁清单,详细的医疗网站用户注册表格之类的内容构建大型表格.现在,我将构建表单,分配ID和名称,并确定需要哪些值,等等.然后,当我为表单构建php表单验证和数据库插入部分时,我一直在手动遍历html并从$ _post数组中提取所有ID以供参考,以进行输入验证和数据库插入.这是非常耗时的,并且是真正的痛苦,常常伴随着键入错误.

To understand more fully what I'm trying to accomplish, let me explain. I build large forms from time to time for things such as property applications, rental listings, detailed medical website user registration forms and such. As I do it now, I build the form, assign the id's and names and decide which values are required and such. Then when I build the php form validation and database insert portion for the form, I've been manually going through the html and pulling out all of the id's to reference from the $_post array for the input validation and database insert. This has been very time consuming and a real pain, often laced with typing errors.

我目前正在处理的表单太大了,我宁愿编写一个javascript函数,使其可以在页面的本地副本上运行以列出所有ID,因此我没有复制并粘贴它们,或将其写下来.然后,我还可以使用javascript循环在id名称周围事件输出php代码,以便我只需要复制列表并轻轻地编辑出id,我不需要...我希望你们得到想法.

The form I'm working on currently is just too big, and I'd much rather write a javascript function that I can run on my local copy of the page to list all of the id's so that I don't have to copy and paste them one by one, or write them down. I could then also use the javascript loop to event print out the php code around the id names so that I'd only have to copy the list and lightly edit out the id's I dodn't need... I hope you guys get the idea.

关于如何将所有ID放入数组的任何建议,或者如果已经有一个数组可以访问并遍历(我在Google上找不到任何内容).此外,任何有关如何通过生成php或使其比我当前方法更快的工作流程来加快大表格生成过程的建议,将不胜感激!

Any suggestions on how I can drop all of the id's into an array, or if there is already an array I can access and loop through (I couldn't find anything on google). Also, any suggestions for how to speed up the process of producing large forms with a work flow that generates the php or makes it quicker than my current method would be greatly appreciated!

推荐答案

现代浏览器上,您可以通过

document.querySelectorAll('*[id]')

应该做的工作.

如果您需要ID为 myElement 的所有后代,请这样做

If you need all descendants of myElement with IDs, then do

myElement.querySelectorAll('*[id]')

如果您要非常小心地排除< span id ="> ,那么

If you want to be really careful to exclude <span id="">, then maybe

document.querySelectorAll('*[id]:not([id=""])')


如果需要与旧版浏览器兼容


If compatibility with older browsers is required

var allElements = document.getElementsByTagName("*");
var allIds = [];
for (var i = 0, n = allElements.length; i < n; ++i) {
  var el = allElements[i];
  if (el.id) { allIds.push(el.id); }
}

应该将所有ID留在 allIds 中.

should leave you with all the IDs in allIds.

如果发现只需要枚举特定表单节点下的ID,则可以将 document.getElementsByTagName 替换为 myFormNode.getElementsByTagName .

If you find you need to just enumerate the IDs under a particular form node, then you can replace document.getElementsByTagName with myFormNode.getElementsByTagName.

如果要同时包含ID和名称,请输入

If you want to include both IDs and NAMEs, then put

else if (el.name) { allIds.push(el.name); }

在上面 if 下方.

这篇关于如何使用javascript枚举文档中的所有html id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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