好吧,我有这个空白的对象数组。 我正在动态地查找网页中的每个节点,并且每个节点都将拥有它自己的对象和属性。 我需要一种方法将我需要的值放入其各自的对象属性中将数据推送到对象数组中
因此,例如,我找到了body
节点。我现在有一个特殊的小对象用于这个节点。我需要把这个小家伙的一切都投入到他的物体的属性中。
所以我非常需要它来呈现这样的:
谈到这一点:
<html>
<body style="margin:0; padding:0;" title="My Title">
<p>some text</p>
<div class="wrapper"></div>
<footer></footer>
</body>
</html>
进入这个:
this.nodesInfo = [ // All nodes in the page's DOM
{
type: 'body', // ex: body, section, aside, div, etc.
id: 'myID', // the Id of that element
class: ['myClass1', 'myClass2'], // the class/class' of that element
depth: '2', // the level in the page's DOM in which that element sits, this will be an integer
parent: 'html', // that elements direct parent Node
children:['div.wrapper', 'p', 'footer'], // any child Nodes that, that element may be a parent to
text: '', // the text inside that element if any exists
attributes: ["style=margin:0; padding:0;", "title='My Title'"] // all attributes of this node
}
];
它将通过每个节点当然周期才发现,和相应地为每个节点执行此操作,直到耗尽节点。
类,儿童,和属性性质对于任何这些倍数的简单的可能性阵列。其他的只是一个字符串,因为节点不能有多个ID,标题或直接父标记。
如果一个节点不包含一些属性则该属性将保持空白/空/未定义。
我的问题很简单。这是否可能,如果不是,我将不得不单独创建每个对象,并将它们推入我的nodesInfo
阵列中?
我觉得去这将使得每个节点的对象,然后将它们推所有(一旦它们都创建)到一个数组的最简单的方法。
为什么你需要这个?有一个名为[The DOM]的资源(https://developer.mozilla.org/en/DOM/About_the_Document_Object_Model)。 – paislee 2012-02-28 06:03:46
我前几天做了这样的事情,查看我的答案。 – elclanrs 2012-02-28 06:23:47
@paislee我正在研究一个项目,以帮助更好地可视化网站DOM地图。我认为在CSS和jQuery交互和操作方面,这将有助于该网站DOM的导航。但感谢无用的评论。 =) – 2012-02-28 06:33:52