Difference between setAttribute and setAttributeNS (null,

What is the difference between calling setAttribute and setAttributeNS with a null namespace parameter?

Also, is there a problem using getAttribute () and then setAttributeNS?

+11
source share
4 answers

setAttribute () is a DOM 1 function. setAttributeNS () is a DOM 2 function that solves the problem of conflicting tag or attribute names by specifying the xmlns namespace that should be applied to the tag / attribute in the first argument.

If the attribute does not have a specific namespace prefix, the first argument must be zero . You can use setAttribute () , but for consistency it is recommended that you stick to setAttributeNS () . Cm:

https://developer.mozilla.org/en/docs/Web/SVG/Namespaces_Crash_Course#Scripting_in_namespaced_XML

β€œHowever, note: the namespace in the XML 1.1 recommendation states that the namespace name for attributes without a prefix does not matter. In other words, although the attributes belong to the tag namespace, you are not using the namespace of the tag name. Instead, you must use zero as the namespace name for unqualified (without prefix) attributes. "

+8
source

The setAttributeNS method is an XML method and will not work with HTML elements.

0
source

setAttributeNS used to specify a namespace and add a new attribute to the namespace. NS represent this. It also requires three parameters.

 element.setAttributeNS(ns,name,value) ns :namespace URI of the attribute to set name:Name of the attribute to set value:Value of the attribute to set setAttribute(name,value) which is use to add a new attribute or change the value of existing attribute. 
0
source

Here is the English explanation from the MDN docs :

 // Given: // <div id="div1" xmlns:special="http://www.mozilla.org/ns/specialspace" // special:specialAlign="utterleft" width="200px" /> d = document.getElementById("div1"); d.removeAttributeNS("http://www.mozilla.org/ns/specialspace", "specialAlign"); // Now: // <div id="div1" width="200px" /> 

So it follows that xmlns:special="http://www.mozilla.org/ns/specialspace" is a special namespace declaration, which is then used to contextualize special:specialAlign .

0
source

Source: https://habr.com/ru/post/1241701/


All Articles