createelement
-
Returns
退货
-
Element
Element
Throws
投掷
-
INVALID_CHARACTER_ERR
INVALID_CHARACTER_ERR
例 (Example)
var element = document.createElement('h1');
element.appendChild(document.createTextNode
('The man who mistook his wife for a hat'));
The example above creates an <h1>
element, and then adds text to it.
上面的示例创建一个<h1>
元素,然后向其中添加文本。
The end result of that operation would be this HTML:
该操作的最终结果将是以下HTML:
<h1>The man who mistook his wife for a hat</h1>
争论 (Arguments)
-
tagname (
标记名 (
-
The
tagName
of the element. In XML this is case-sensitive; in HTML the name can be specified in any case, but will be converted to the canonical upper-case form of HTML tag names.元素的
tagName
。 在XML中,这是区分大小写的。 在HTML中,可以在任何情况下指定名称,但会将其转换为HTML标记名称的规范大写形式。
DOMString
DOMString
required
需要
描述 (Description)
Create an Element
node of the specified type.
创建指定类型的Element
节点。
The created element can then be added to the document using Node
methods such as appendChild
or insertBefore
.
然后可以使用Node
方法(例如appendChild
或insertBefore
将创建的元素添加到文档中。
A created element implements the Element
interface as soon as it’s created, so attributes can be added to it immediately, without having to append it to the document first. If the element has default attributes in this document type, those attributes are automatically created and attached to the element.
创建的元素会在创建后立即实现Element
接口,因此可以立即向其添加属性,而不必先将其附加到文档中。 如果元素在此文档类型中具有默认属性,则将自动创建这些属性并将其附加到元素。
This method creates non-namespaced elements; to create a namespaced element, use the DOM 2 “createElementNS”createElementNS
method instead.
此方法创建未命名的元素; 要创建命名空间元素,请改用DOM 2“ createElementNS” createElementNS
方法。
返回值 (Return value)
The created element node, with its nodeName
set to the specified tag name, and its localName
, prefix
and namespaceURI
set to null
创建的元素节点,其nodeName
设置为指定的标记名称,其localName
, prefix
和namespaceURI
设置为null
翻译自: https://www.sitepoint.com/createelement-w3c-dom-core-method/
createelement