Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Document.createElementNS()

这篇翻译不完整。请帮忙从英语翻译这篇文章

Creates an element with the specified namespace URI and qualified name. 

Syntax

element = document.createElementNS(namespaceURI, qualifiedName);
  • element is the created element.
  • namespaceURI is a string that specifies the namespace URI to associate with the element. The namespaceURI property of the created element is initialized with the value of namespaceURI. (see section below for "Valid Namespace URI's")
  • qualifiedName is a string that specifies the type of element to be created. The nodeName property of the created element is initialized with the value of qualifiedName

Valid Namespace URI's

  • HTML - Use https://www.w3.org/1999/xhtml
  • SVG - Use https://www.w3.org/2000/svg
  • XBL - Use https://www.mozilla.org/xbl
  • XUL - Use https://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul

Example

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="https://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
      xmlns:html="https://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("https://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>

Notes

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. Inline script does not cause any problems for this short example, however, for any serious work you should learn about Properly Using CSS and JavaScript in XHTML Documents.

To create an element without specifying a namespace URI, use the createElement method.

Specification

See also

文档标签和贡献者

 此页面的贡献者: FredWe
 最后编辑者: FredWe,