Javascript createElement 与 createElementNS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8173217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
createElement vs. createElementNS
提问by CoR
What's the real difference between those two? I mean real, essential difference.
What's the future holding for regular createElement
?
这两者之间的真正区别是什么?我的意思是真正的、本质的区别。定期持有的未来是createElement
什么?
Svg is xml, not html. I get that. So we use createElementNS(ns_string, 'svg')
And then setAttributeNS(null,,)
. Why?
Why not setAttributeNS('my_ns',,)
?
Svg 是 xml,而不是 html。我明白了。所以我们使用createElementNS(ns_string, 'svg')
And then setAttributeNS(null,,)
。为什么?为什么不setAttributeNS('my_ns',,)
呢?
Why must ns_string
be http://www.w3.org/2000/svg
and not some random string? What's the purpose of a namespace if there is only one namespace?
为什么必须ns_string
是http://www.w3.org/2000/svg
而不是一些随机字符串?如果只有一个命名空间,命名空间的目的是什么?
What's the purpose of ns
in regular html? Should I change all instances of createElement
to createElementNS
in my existing code?
ns
在常规 html 中的目的是什么?我应该在现有代码中更改createElement
to 的所有实例createElementNS
吗?
I am reading the DOM-Level-2spec. but I'm still puzzled.
我正在阅读DOM-Level-2规范。但我还是很困惑。
采纳答案by Mike Samuel
To understand the problem namespaces are trying to solve, consider file extensions. 3-letter file extensions have done a really bad job of describing the content of files. They're ambiguous and don't carry version info. XML namespaces use a larger space of strings, URIs, to solve the same problem, and use short prefixes so you can succinctly mix multiple kinds of XML in the same document.
要了解命名空间试图解决的问题,请考虑文件扩展名。3 个字母的文件扩展名在描述文件内容方面做得非常糟糕。它们是模棱两可的,不携带版本信息。XML 命名空间使用更大空间的字符串、URI 来解决相同的问题,并使用短前缀以便您可以在同一文档中简洁地混合多种 XML。
What's the purpose of namespace if there is only one name space?
如果只有一个命名空间,命名空间的目的是什么?
There are many namespaces used to identify different kinds of XML, and different versions of those kind.
有许多名称空间用于标识不同种类的 XML,以及这些种类的不同版本。
SVG and MathML are two kinds of XML each with their own namespaces that can be embedded in HTML5, and they often use XLink, another XML namespace. Many other XML schemas, with corresponding namespaces, are used for passing messages between clients and servers and for data storage.
SVG 和 MathML 是两种 XML,每种都有自己的命名空间,可以嵌入到 HTML5 中,它们经常使用 XLink,另一个 XML 命名空间。许多其他具有相应名称空间的 XML 模式用于在客户端和服务器之间传递消息以及用于数据存储。
XHTML is an attempt to express HTML as valid XML. It has its own namespace.
XHTML 试图将 HTML 表示为有效的 XML。它有自己的命名空间。
So we use createElementNS(ns_string, 'svg') And then setAttributeNS(null,,). Why? Why not setAttributeNS('my_ns',,)???
所以我们使用 createElementNS(ns_string, 'svg') 然后 setAttributeNS(null,,)。为什么?为什么不 setAttributeNS('my_ns',,)???
You should probably try to consistently use setAttributeNS
with a namespace URI when using createElementNS
with a namespace URI.
setAttributeNS
与命名空间 URIcreateElementNS
一起使用时,您可能应该尝试始终与命名空间 URI 一起使用。
XML was defined in multiple steps. The first version of the spec said nothing about namespaces but left enough syntax so that XML with namespaces could be specified on top of XML without namespaces by using prefixes and special xmlns
attributes. The XML specificationsays:
XML 是在多个步骤中定义的。规范的第一个版本没有提及名称空间,但保留了足够的语法,以便可以通过使用前缀和特殊xmlns
属性在没有名称空间的 XML 之上指定带有名称空间的 XML 。该XML规范说:
"The Namespaces in XML Recommendation [XML Names] assigns a meaning to names containing colon characters. Therefore, authors should not use the colon in XML names except for namespace purposes, but XML processors must accept the colon as a name character."
“XML 建议中的命名空间 [XML 名称] 为包含冒号字符的名称分配了含义。因此,作者不应在 XML 名称中使用冒号,除非是出于命名空间的目的,但 XML 处理器必须接受冒号作为名称字符。”
XML namespaces let XML processing applications know what they're dealing with, and allow multiple kinds of XML to be mixed together in the same document.
XML 名称空间让 XML 处理应用程序知道它们正在处理什么,并允许在同一个文档中将多种 XML 混合在一起。
Why ns_string must be "http://www.w3.org/2000/svg"
为什么 ns_string 必须是“ http://www.w3.org/2000/svg”
This includes the year that version of SVG was standardized, 2000, so it carries useful information.
这包括 SVG 版本标准化的年份,2000,因此它包含有用的信息。
When used with xmlns:svg
it also lets the browser know that the svg:
prefix means SVG and not some other dialect of XML.
与xmlns:svg
它一起使用时,还可以让浏览器知道svg:
前缀表示 SVG 而不是 XML 的其他方言。