您的位置:首页 > Web前端 > JavaScript

全栈JavaScript之路( 二十三 )DOM2、DOM3, 涉及XML命名空间的扩展(一)

2014-07-10 11:11 441 查看
<!DOCTYPE html>
<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml">
<head>
<title>Example XHTML page</title>
</head>
<body>
<s:svg xmlns:s="http://www.w3.org/2000/svg" version="1.1" style="width:100%; height:100%; viewBox:0 10 10 10">
<s:rect x="0" y="0" width="100" height="100" style="fill:red"/>
</s:svg>
</body>
</xhtml:html>


以上例子展示,通过 xmlns 来指定命名空间,通过 xmlns:prefix 来指定 前缀,一但指定的前缀,就得在当前元素,以及子元素使用。

有时候为了避免不同语言间的冲突,也需要使用命名空间来限定特性,如下面的例子所示。

<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xhtml:head>
<xhtml:title>Example XHTML page</xhtml:title>
</xhtml:head>
<xhtml:body xhtml:class="home">
Hello world!
</xhtml:body>
</xhtml:html>


(一)Node类型的变化:
在DOM2中 Node类型包含了下表特定于命名空间的特性

localName
prefix
namespaceURL

在DOM3中,更进一步增加了下列属性和方法

isDefaultNamaspace(namespaceURL)
lookupNamespaceURL(prefix)
lookupPrefix(namespaceURL)

(二)Document 类型的变化

createElementNS(namespaceURL,tagName)

createAttributeNS(namespaceURL,tagName)

getElementByTagName(namespaceURL,tagName)

只有文档中存在两个或多个命名空间的时候,这些跟命名空间相关的方法才是必须的。

(三)Element 类型的变化

       DOM core 中 ,涉及命名空间的变化 ,主要是操作特性的方法。

getAttributeNS(namespaceURL,localName)

getAttributeNodeNS(namespaceURL,localName)

getElementByTagName(namespaceURL,tagName)

hasAttribute(namespaceURL,tagName)  //dom 也同时增加了 hasAttribute()

removeAttributeNS(namespaceURL,localName)

setAttributeNS(namespaceURL,localName,value)

setAttributeNodeNS(namespaceURL,attNode)

除了第一个参数之外,这些方法与DOM1 级中相关方法的作用相同;第一个参数始终都是一个命名空间URI。

(四) NamedNodeMap 类型的变化

getNamedItemNS(namespaceURL,localName)

setNamedItemNS(node)

removeNamedItemNS(namespaceURL,localName)

由于一般都是通过元素访问特性,所以这些方法很少使用。

        
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xhtml xml namascpce