15.4.2 Getting and Setting Non-HTML Attributes
As described above, HTMLElement and its subtypes define properties that correspond to the standard attributes of HTML elements. The Element type also defines getAttribute() and setAttribute() methods that you can use to query and set nonstandard HTML attributes and to query and set attributes on the elements of an XML document:
var image = document.images; var width = parseInt(image.getAttribute("WIDTH")); image.setAttribute("class", "thumbnail");
Element also defines two related methods, hasAttribute() and removeAttribute(), which check for the presence of a named attribute and remove an attribute entirely. These methods are particularly useful with boolean attributes: these are attributes (such as the disabled attribute of HTML form elements) whose presence or absence from an element matters but whose value is not relevant.
If you are working with XML documents that include attributes from other namespaces, you can use the namespaced variants of these four methods: getAttributeNS(), setAttributeNS(), hasAttributeNS(), and removeAttributeNS(). Instead of taking a single attribute name string, these methods take two. The first is the URI that identifies the namespace. The second is usually the unqualified local name of the attribute within the namespace. For setAttributeNS() only, however, the second argument is the qualified name of the attribute and includes the namespace prefix. You can read more about these namespace-aware attribute methods in Part IV .欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31426.html