--- title: Document.createElementNS() slug: Web/API/Document/createElementNS translation_of: Web/API/Document/createElementNS ---
지정된 네임스페이스 URI와 적합한 이름으로 엘리먼트를 만든다.
네임스페이스 URI를 지정하지 않고 엘리먼트를 만들려면 createElement메소드를 사용하라.
var element = document.createElementNS(namespaceURI, qualifiedName[, options]);
namespaceURI
namespaceURI
. See Valid Namespace URIs.qualifiedName
qualifiedName
.options
OptionalElementCreationOptions
object containing a single property named is
, whose value is the tag name for a custom element previously defined using customElements.define()
. For backwards compatibility with previous versions of the Custom Elements specification, some browsers will allow you to pass a string here instead of an object, where the string's value is the custom element's tag name. See Extending native HTML elements for more information on how to use this parameter.is
attribute whose value is the custom element's tag name. Custom elements are an experimental feature only available in some browsers.The new Element
.
http://www.w3.org/1999/xhtml
http://www.w3.org/2000/svg
http://www.mozilla.org/xbl
http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul
This creates a new <div> element in the XHTML namespace and appends it to the vbox element. Although this is not an extremely useful XUL document, it does demonstrate the use of elements from two different namespaces within a single document:
<?xml version="1.0"?> <page xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:html="http://www.w3.org/1999/xhtml" title="||Working with elements||" onload="init()"> <script type="text/javascript"><![CDATA[ var container; var newdiv; var txtnode; function init(){ container = document.getElementById("ContainerBox"); newdiv = document.createElementNS("http://www.w3.org/1999/xhtml","div"); txtnode = document.createTextNode("This is text that was constructed dynamically with createElementNS and createTextNode then inserted into the document using appendChild."); newdiv.appendChild(txtnode); container.appendChild(newdiv); } ]]></script> <vbox id='ContainerBox' flex='1'> <html:div> The script on this page will add dynamic content below: </html:div> </vbox> </page>
The example given above uses inline script which is not recommended in XHTML documents. This particular example is actually an XUL document with embedded XHTML, however, the recommendation still applies.
Specification | Status | Comment |
---|---|---|
{{SpecName('DOM WHATWG', "#dom-document-createelementns", "Document.createElement")}} | {{Spec2('DOM WHATWG')}} |
{{Compat("api.Document.createElementNS")}}