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

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

问题描述

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



为了更全面地理解我想要完成的任务,请让我解释一下。我不时为物业申请,出租物品清单,详细的医疗网站用户登记表格等建立大型表格。正如我现在这样做,我建立表格,分配ID和名称,并决定哪些值是必需的,等等。然后,当我为表单构建php表单验证和数据库插入部分时,我一直手动浏览html并从$ _post数组中提取所有id以引用输入验证和数据库插入。这非常耗费时间,而且非常痛苦,常常伴随着打字错误。

目前我正在处理的表单太大了,我宁愿写一个javascript函数,我可以在我的本地副本上运行列出所有的ID,以便我不必一一复制和粘贴,或写下来。然后,我可以使用JavaScript循环来打印出围绕id名称的php代码,这样我只需要复制列表并轻轻地编辑出id就可以了,我不需要...我希望你们能够得到想法。

关于如何将所有的id放入数组,或者如果已经有一个数组我可以访问并循环访问(我不能在谷歌上找到任何东西)。另外,任何关于如何加快产生大量表单的工作流程来生成php或者比我目前的方法更快的建议,我们将不胜感激!

解决方案

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

  document.querySelectorAll('* [id]')

应该完成这项工作。



如果您需要 myElement 带ID,然后做

$ p $ myElement.querySelectorAll('* [id]')

如果您想非常小心地排除< span id => ,然后可能是

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






如果需要与旧浏览器兼容

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

应该为您提供 allIds中的所有ID



如果您发现只需枚举特定表单节点下的ID,则可以替换文档.getElementsByTagName myFormNode.getElementsByTagName



如果您想包含ID和NAME,然后放入

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

低于 if if p>

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.

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.

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.

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!

解决方案

On modern browsers you can do this via

document.querySelectorAll('*[id]')

should do the job.

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

myElement.querySelectorAll('*[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); }
}

should leave you with all the IDs in allIds.

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

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

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

below the if above.

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

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