createElement 与 createElementNS [英] createElement vs. createElementNS

查看:29
本文介绍了createElement 与 createElementNS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两者之间的真正区别是什么?我的意思是真正的、本质的区别.常规 createElement 的未来是什么?

What's the real difference between those two? I mean real, essential difference. What's the future holding for regular createElement?

Svg 是 xml,而不是 html.我明白了.所以我们使用 createElementNS(ns_string, 'svg')然后 setAttributeNS(null,,).为什么?为什么不 setAttributeNS('my_ns',,)?

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',,)?

为什么 ns_string 必须是 http://www.w3.org/2000/svg 而不是一些随机字符串?如果只有一个命名空间,命名空间有什么用?

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 在常规 html 中的用途是什么?我是否应该将现有代码中 createElement 的所有实例更改为 createElementNS?

What's the purpose of ns in regular html? Should I change all instances of createElement to createElementNS in my existing code?

我正在阅读 DOM-Level-2 规范.但我还是不解.

I am reading the DOM-Level-2 spec. but I'm still puzzled.

推荐答案

要了解命名空间试图解决的问题,请考虑文件扩展名.3 个字母的文件扩展名在描述文件内容方面做得非常糟糕.它们是模棱两可的,不携带版本信息.XML 命名空间使用更大空间的字符串、URI 来解决相同的问题,并使用短前缀以便您可以在同一个文档中简洁地混合多种 XML.

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.

如果只有一个命名空间,命名空间的目的是什么?

What's the purpose of namespace if there is only one name space?

有许多命名空间用于标识不同种类的 XML,以及这些种类的不同版本.

There are many namespaces used to identify different kinds of XML, and different versions of those kind.

SVG 和 MathML 是两种 XML,每种都有自己的命名空间,可以嵌入到 HTML5 中,它们经常使用另一个 XML 命名空间 XLink.许多其他具有相应名称空间的 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.

XHTML 试图将 HTML 表达为有效的 XML.它有自己的命名空间.

XHTML is an attempt to express HTML as valid XML. It has its own namespace.

所以我们使用 createElementNS(ns_string, 'svg') 然后 setAttributeNS(null,,).为什么?为什么不 setAttributeNS('my_ns',,)???

So we use createElementNS(ns_string, 'svg') And then setAttributeNS(null,,). Why? Why not setAttributeNS('my_ns',,)???

在将 createElementNS 与命名空间 URI 一起使用时,您可能应该尝试一致地将 setAttributeNS 与命名空间 URI 一起使用.

You should probably try to consistently use setAttributeNS with a namespace URI when using createElementNS with a namespace URI.

XML 是在多个步骤中定义的.规范的第一个版本没有提及名称空间,但留下了足够的语法,以便可以通过使用前缀和特殊的 xmlns 属性在没有名称空间的 XML 之上指定带有名称空间的 XML.XML 规范 说:

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 specification says:

XML 建议中的命名空间 [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 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.

为什么 ns_string 必须是 "http://www.w3.org/2000/svg"

Why ns_string must be "http://www.w3.org/2000/svg"

这包括 SVG 版本标准化的年份,2000,所以它包含有用的信息.

This includes the year that version of SVG was standardized, 2000, so it carries useful information.

当与 xmlns:svg 一起使用时,它还让浏览器知道 svg: 前缀表示 SVG,而不是其他一些 XML 方言.

When used with xmlns:svg it also lets the browser know that the svg: prefix means SVG and not some other dialect of XML.

这篇关于createElement 与 createElementNS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆