Skip to content

命名空间

¥Namespaces

XML 命名空间很有趣!对吧?🤪 幸运的是,你基本上可以忽略它们。

¥XML namespaces are fun! Right? 🤪 Fortunately you can mostly ignore them.

需要指定它们的情况是,将元素附加到属于不同命名空间的父元素时;通常,要在 SVG foreignObject 元素内创建 div

¥A case where you need to specify them is when appending an element to a parent that belongs to a different namespace; typically, to create a div inside a SVG foreignObject element:

js
d3.create("svg")
  .append("foreignObject")
    .attr("width", 300)
    .attr("height", 100)
  .append("xhtml:div")
    .text("Hello, HTML!");

namespace(name)

源代码 · 限定指定的名称,该名称可以有也可以没有命名空间前缀。

¥Source · Qualifies the specified name, which may or may not have a namespace prefix.

js
d3.namespace("svg:text") // {space: "http://www.w3.org/2000/svg", local: "text"}

如果名称包含冒号(:),则冒号前的子字符串将被解释为命名空间前缀,该前缀必须在 d3.namespaces 中注册。返回一个对象,该对象包含 spacelocal 属性,用于描述完整的命名空间 URL 和本地名称。如果名称不包含冒号,则此函数仅返回输入名称。

¥If the name contains a colon (:), the substring before the colon is interpreted as the namespace prefix, which must be registered in d3.namespaces. Returns an object space and local attributes describing the full namespace URL and the local name. If the name does not contain a colon, this function merely returns the input name.

namespaces

源代码 · 已注册命名空间前缀的映射。初始值为:

¥Source · The map of registered namespace prefixes. The initial value is:

js
{
  svg: "http://www.w3.org/2000/svg",
  xhtml: "http://www.w3.org/1999/xhtml",
  xlink: "http://www.w3.org/1999/xlink",
  xml: "http://www.w3.org/XML/1998/namespace",
  xmlns: "http://www.w3.org/2000/xmlns/"
}

可以根据需要分配其他前缀,以便在其他命名空间中创建元素或属性。

¥Additional prefixes may be assigned as needed to create elements or attributes in other namespaces.