Latest web development tutorials

XML DOM setAttributeNS () method

Element Object Reference Element object

Definition and Usage

setAttributeNS () method to add a new attribute (with a namespace).

If the element already exists in the specified attribute or namespace name, its value is changed to the value of the prefix and value parameters.

grammar

elementNode.setAttributeNS(ns,name,value)

参数 描述
ns 必需。规定要设置的属性的命名空间 URI。
name 必需。规定要设置的属性的名称。
value 必需。规定要设置的属性的值。


Example 1

The following code fragment uses loadXMLDoc () to " books_ns.xml " into xmlDoc to the first <book> element to "edition" attribute:

Examples

xmlDoc=loadXMLDoc("books_ns.xml");

x=xmlDoc.getElementsByTagName("book")[0];
ns="http://www.w3schools.com/edition/";

x.setAttributeNS(ns,"edition","first");

document.write(x.getAttributeNS(ns,"edition"));

Output:

first

try it"

Example 2

The following code fragment uses loadXMLDoc () to " books_ns.xml " into xmlDoc, and change the first <title> element of the "lang" value:

Examples

xmlDoc=loadXMLDoc("books_ns.xml");

x=xmlDoc.getElementsByTagName("title")[0];
ns="http://www.w3schools.com/children/";

x.setAttributeNS(ns,"c:lang","italian");

document.write(x.getAttributeNS(ns,"lang"));

Output:

italian

try it"

Element Object Reference Element object