DIV节点如何被“分离”有什么用? [英] How can a DIV node been "detached" and what's the use?

查看:145
本文介绍了DIV节点如何被“分离”有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本文中,创建了一个分离的Div节点:$ b​​ $ b http://www.bennadel.com/blog/1008-jQuery-empty-Kills-Event-Binding-On-Persistent-Nodes.htm



我不明白:我认为DOM拥有每个节点。那么你将如何附加到DOM?



最后但并非最不重要的是分离节点的目的是什么?

解决方案

我不知道你期望哪个答案,所以这里有一些想法:


我认为DOM拥有每个节点。


文档拥有每个节点。每个节点都有一个 ownerDocument [MDN] 属性。



规范


文档 interface表示整个HTML或XML文档。在概念上,它是文档树的根,并提供对文档数据的主要访问。



由于元素,文本节点,注释,处理指令等不能存在于文档的上下文之外, Document 界面还包含创建这些对象所需的工厂方法。创建的节点对象具有一个 ownerDocument 属性,将它们与文档在其上下文中创建。








你将如何附加到DOM? / p>

有多种方法可以插入新的节点,例如 appendChild [docs] 或< a href =https://developer.mozilla.org/En/DOM/Node.insertBefore =noreferrer> insertBefore [docs ]







最后但不是最小的是分离节点的目的是什么?


一个优点是可以建立复杂的子树离线,以便浏览器不必每次插入一个节点时重新计算布局。



有时它也有助于解析HTML字符串。通过创建一个空的分开的 div 并将HTML字符串分配给 innerHTML ,您可以轻松地解析和处理HTML字符串



唯一的注意事项是, document.getElementById 找不到不属于树的节点。 p>




在这方面也很有趣可能是对 Node.parentNode 财产。毕竟,没有父项的 Node 不是树的一部分:


此节点的父节点。所有节点,除了 Attr 文档 DocumentFragment 实体符号可能有一个父级。但是,如果刚刚创建了一个节点,但尚未添加到树中,或者如果已从树中删除了节点,则此节点为 null



In this article a detached Div node is created: http://www.bennadel.com/blog/1008-jQuery-empty-Kills-Event-Binding-On-Persistent-Nodes.htm

I don't understand : I thought that DOM owns every nodes. How would you attach to DOM then ?

Last but not least what would be the purpose of having detached node ?

解决方案

I'm not sure which answer you expect, so here are some thoughts:

I thought that DOM owns every nodes.

The document owns every node. Each node has an ownerDocument [MDN] property.

From the specification:

The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.

Since elements, text nodes, comments, processing instructions, etc. cannot exist outside the context of a Document, the Document interface also contains the factory methods needed to create these objects. The Node objects created have a ownerDocument attribute which associates them with the Document within whose context they were created.


How would you attach to DOM then?

There are various ways to insert a new node, such as appendChild [docs] or insertBefore [docs].


Last but not least what would be the purpose of having detached node ?

One advantage is that you can build complex subtrees offline so that the browser does not have to recalculate the layout every time you insert a node.

Sometimes it is also useful for parsing an HTML string. By creating an empty, detached div and assign the HTML string to innerHTML, you can parse and process the HTML string easily.

The only caveat is that document.getElementById cannot find nodes which are not part of the tree.


Also interesting in this regard might be the explanation for the Node.parentNode property. After all, a Node which does not have parent is not part of the tree:

The parent of this node. All nodes, except Attr, Document, DocumentFragment, Entity, and Notation may have a parent. However, if a node has just been created and not yet added to the tree, or if it has been removed from the tree, this is null.

这篇关于DIV节点如何被“分离”有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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