jscript与vbscript 操作XML元素属性的代码
程序员文章站
2022-03-21 23:38:15
although attributes belong to a particular element, they are not considered child node...
although attributes belong to a particular element, they are not considered child nodes of element nodes. instead, they behave more like properties of ixmldomelement.
most of the methods for working with attributes come from ixmldomelement. attributes can be manipulated in the following ways.
directly, through the getattribute and setattribute methods of ixmldomelement.
as named ixmldomattribute nodes, with getattributenode and setattributenode.
as a set of nodes accessible through the attributes property and returned as an ixmlnamednodemap.
examples
jscript
the following jscript example creates a new document containing a <memo> element, and then creates an attribute named author with a value of "pat coleman".
var xmldoc = new activexobject("msxml2.domdocument.3.0");
var rootelement=xmldoc.createelement("memo");
rootelement.setattribute("author", "pat coleman");
xmldoc.appendchild(rootelement);
vbscript
set xmldoc = createobject("msxml2.domdocument.3.0")
set rootelement=xmldoc.createelement("memo")
rootelement.setattribute("author", "pat coleman")
xmldoc.appendchild(rootelement)
if you prefer to work with attribute nodes, you can create the attribute, and then create a text node to store its value. attribute nodes can only contain text nodes and entity reference nodes. (if you need to create an attribute containing an entity reference, you must use this approach.)
working with attribute nodes requires using the domdocument object to create attribute and text (and entity reference, if necessary) nodes before assigning the nodes to the element.
jscript
the following jscript code uses this approach to perform the same work as the preceding examples, creating a <memo> element with an author attribute holding the value "pat coleman".
var xmldoc = new activexobject("msxml2.domdocument.3.0");
var rootelement=xmldoc.createelement("memo");
var memoattribute=xmldoc.createattribute("author");
var memoattributetext=xmldoc.createtextnode("pat coleman");
memoattribute.appendchild(memoattributetext);
rootelement.setattributenode(memoattribute);
xmldoc.appendchild(rootelement);
vbscript
set xmldoc = createobject("msxml2.domdocument.3.0")
set rootelement=xmldoc.createelement("memo")
set memoattribute=xmldoc.createattribute("author")
set memoattributetext=xmldoc.createtextnode("pat coleman")
memoattribute.appendchild(memoattributetext)
rootelement.setattributenode(memoattribute)
xmldoc.appendchild(rootelement)
most of the methods for working with attributes come from ixmldomelement. attributes can be manipulated in the following ways.
directly, through the getattribute and setattribute methods of ixmldomelement.
as named ixmldomattribute nodes, with getattributenode and setattributenode.
as a set of nodes accessible through the attributes property and returned as an ixmlnamednodemap.
examples
jscript
the following jscript example creates a new document containing a <memo> element, and then creates an attribute named author with a value of "pat coleman".
复制代码 代码如下:
var xmldoc = new activexobject("msxml2.domdocument.3.0");
var rootelement=xmldoc.createelement("memo");
rootelement.setattribute("author", "pat coleman");
xmldoc.appendchild(rootelement);
vbscript
复制代码 代码如下:
set xmldoc = createobject("msxml2.domdocument.3.0")
set rootelement=xmldoc.createelement("memo")
rootelement.setattribute("author", "pat coleman")
xmldoc.appendchild(rootelement)
if you prefer to work with attribute nodes, you can create the attribute, and then create a text node to store its value. attribute nodes can only contain text nodes and entity reference nodes. (if you need to create an attribute containing an entity reference, you must use this approach.)
working with attribute nodes requires using the domdocument object to create attribute and text (and entity reference, if necessary) nodes before assigning the nodes to the element.
jscript
the following jscript code uses this approach to perform the same work as the preceding examples, creating a <memo> element with an author attribute holding the value "pat coleman".
复制代码 代码如下:
var xmldoc = new activexobject("msxml2.domdocument.3.0");
var rootelement=xmldoc.createelement("memo");
var memoattribute=xmldoc.createattribute("author");
var memoattributetext=xmldoc.createtextnode("pat coleman");
memoattribute.appendchild(memoattributetext);
rootelement.setattributenode(memoattribute);
xmldoc.appendchild(rootelement);
vbscript
复制代码 代码如下:
set xmldoc = createobject("msxml2.domdocument.3.0")
set rootelement=xmldoc.createelement("memo")
set memoattribute=xmldoc.createattribute("author")
set memoattributetext=xmldoc.createtextnode("pat coleman")
memoattribute.appendchild(memoattributetext)
rootelement.setattributenode(memoattribute)
xmldoc.appendchild(rootelement)
上一篇: 干鱼肚可以放多久?干鱼肚多少钱一斤?
下一篇: PHP实现递归的三种方法
推荐阅读
-
操作Dom中的子元素与兄弟元素的代码
-
jscript与vbscript 操作XML元素属性的代码
-
从零开始学习jQuery (四) jQuery中操作元素的属性与样式_jquery
-
优雅的获取表单元素、dom树的遍历与常用属性、dom元素的增删改操作、js操作元素内容的几个常用方法、元素的dataset对象、获取元素的计算样式、元素的classList 对象常用方法、事件的添加与派发
-
从零开始学习jQuery (四) jQuery中操作元素的属性与样式_jquery
-
优雅的获取表单元素、dom树的遍历与常用属性、dom元素的增删改操作、js操作元素内容的几个常用方法、元素的dataset对象、获取元素的计算样式、元素的classList 对象常用方法、事件的添加与派发
-
关于操作XML元素属性的详细介绍
-
jQuery如何操作HTML的元素和属性?(代码详解例)
-
通过js获取元素表单以及遍历dom树和对元素的增删改查js操作内容演练实战了留言板的操作并对自定义属性: dataset对象初步的了解以及对js操作class和classList属性对象事件的添加与删除练习与了解
-
表单元素获取,dom树的遍历与常用属性,dom元素的增删改, js操作元素内容,留言板实例,dataset对象,获取元素计算样式,classList 对象常用方法 ,事件的添加与派发