[英]JavaScript creating HTML5/XML element node
To create an element to be used in a HTML5 document with JavaScript we can use the createElement() and createElementNS() methods from DOMDocument objects: 要使用JavaScript创建要在HTML5文档中使用的元素,我们可以使用DOMDocument对象中的createElement()和createElementNS()方法:
var node = document.createElement(element);
var node = document.createElementNS(nameSpace, element);
These commands create a node and than we can put it at some point of the document tree using appendChild()
. 这些命令创建一个节点,然后我们可以使用
appendChild()
将其放置在文档树的某个位置。 My first question is: do the document.createElement()
and document.createElementNS()
functions store the node object they return at some place in the document
object? 我的第一个问题是:
document.createElement()
和document.createElementNS()
函数是否将它们返回的节点对象存储在document
对象中的某个位置?
Second, is it possible to create an element node object (possibly using a name space) independent of specific document
objects and later on to append it in the document tree of some document
object given? 其次,是否可以创建独立于特定
document
对象的元素节点对象(可能使用名称空间),然后再将其附加到给定的某些document
对象的文档树中? That is, are there functions similar to createElement()
and createElementNS()
which are not members of a specific object (like the document
object, as is the case above)? 也就是说,是否存在类似于
createElement()
和createElementNS()
函数,它们都不是特定对象的成员(例如document
对象,如上面的情况)?
Third, is it possible to create an element node object using a given document object document1
and them to append it on the document tree of another document object document2
? 第三,是否可以使用给定的文档对象
document1
创建元素节点对象,并将它们附加到另一个文档对象document2
的文档树上?
My first question is: do the document.createElement() and document.createElementNS() functions store the node object they return at some place in the document object?
我的第一个问题是:document.createElement()和document.createElementNS()函数是否将它们返回的节点对象存储在文档对象中的某个位置?
No, the implementation may do something like that internally, but nothing like that is exposed externally. 不,实现可能在内部执行类似的操作,但在外部没有执行任何类似的操作。
is it possible to create an element node object (possibly using a name space) independent of specific document objects and later on to append it in the document tree of some documentobject given?
是否可以创建独立于特定文档对象的元素节点对象(可能使用名称空间),然后将其附加到给定的某些文档对象的文档树中?
The answer to this question is also no: each element, in general, each node is bound to the document object used to create it, which is referenced by the ownerDocument
property. 这个问题的答案也不是:通常,每个元素都绑定到用于创建它的文档对象,该文档对象由
ownerDocument
属性引用。
Third, is it possible to create an element node object using a given document object document1 and them to append it on the document tree of another document object document2?
第三,是否可以使用给定的文档对象document1创建元素节点对象,并将它们附加到另一个文档对象document2的文档树上?
Yes, the second document has to adopt the node, via the adoptNode
method. 是的,第二个文档必须通过
adoptNode
方法采用该节点。 With DOM4 this method will mostly become unnecessary, since it's primarily used before insert a foreign node in a document, and DOM4 specifies that the adoption in such case is implicit. 使用DOM4时,该方法将几乎不再需要,因为它主要在插入文档中的外部节点之前使用,并且DOM4指定在这种情况下采用隐式方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.