如何获取JS中的所有childNodes,包括所有'孙子'? [英] How to get all childNodes in JS including all the 'grandchildren'?

查看:982
本文介绍了如何获取JS中的所有childNodes,包括所有'孙子'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扫描所有childNodes的div,包括那些位于其他元素中的childNodes。现在我有这个:

I want to scan a div for all childNodes including the ones that are nestled within other elements. Right now I have this:

var t = document.getElementById('DivId').childNodes;
for(i=0; i<t.length; i++) alert(t[i].id);

但它只能得到Div的孩子而不是孙子。谢谢!

But it only gets the children of the Div and not the grandchildren. Thanks!

编辑:这个问题太模糊了。对于那个很抱歉。这是一个小提琴:

This question was too vague. Sorry about that. Here's a fiddle:

http://jsfiddle.net/F6L2B /

body.onload脚本不在JSFiddle上运行,但是它有效,除了'Me Second'和'Me Third'输入字段没有被分配一个tabIndex,因此被跳过。

The body.onload script doesn't run at JSFiddle, but it works, except that the 'Me Second' and 'Me Third' input fields are not being assigned a tabIndex and are therefore being skipped over.

推荐答案

这是最快最简单的方法,它可以工作所有浏览器:

This is the fastest and simplest way, and it works on all browsers:

myDiv.getElementsByTagName("*")

这篇关于如何获取JS中的所有childNodes,包括所有'孙子'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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