Element.setAttributeNS()

setAttributeNS añade un nuevo atributo o cambia el valor de un atributo con el namespace dado y el nombre.

Sintaxis

js
elemento.setAttributeNS(namespace, nombre, valor);
  • namespace es un string especificando el namespace del atributo.
  • name es un string identificando el atributo a ser puesto.
  • value es el string del valor deseado del nuevo atributo.

Ejemplo

js
var d = document.getElementById("d1");
d.setAttributeNS("http://www.mozilla.org/ns/specialspace", "align", "center");

Notas

Métodos DOM que tratan con atributos de elementos:

Not namespace-aware, most commonly used methods Namespace-aware variants (DOM Level 2) DOM Level 1 methods for dealing with Attr nodes directly (seldom used) DOM Level 2 namespace-aware methods for dealing with Attr nodes directly (seldom used)
setAttribute (DOM 1) setAttributeNS setAttributeNode (en-US) setAttributeNodeNS (en-US)
getAttribute (DOM 1) getAttributeNS (en-US) getAttributeNode (en-US) getAttributeNodeNS
hasAttribute (DOM 2) hasAttributeNS (en-US) - -
removeAttribute (DOM 1) removeAttributeNS (en-US) removeAttributeNode (en-US) -

Especificaciones

Specification
DOM Standard
# ref-for-dom-element-setattributens①

Compatibilidad con navegadores

BCD tables only load in the browser