DOM
The getAttributeNames() method of the
ProcessingInstruction interface returns the attribute names of the processing instruction as an
Array of strings. If the processing instruction has no attributes it returns an empty
array.
Syntax
getAttributeNames()
Parameters
None.
Return value
An Array of strings.
Description
Using getAttributeNames() along with
ProcessingInstruction.getAttribute is a memory-efficient and
performant alternative to accessing ProcessingInstruction.data.
The names returned by getAttributeNames() are qualified attribute names, meaning that attributes with a namespace prefix have their names returned with that namespace prefix (not the actual namespace), followed by a colon, followed by the attribute name (for example, xlink:href). Any attributes without a namespace prefix have their names returned as-is (for example, href).
Examples
Basic usage
const pi = document.createProcessingInstruction(
"start",
'name="placeholder" more="info"',
);
console.log(pi.getAttributeNames());
// logs:
// ['name', 'more']
// Iterate over processing instruction's attributes
for (const name of pi.getAttributeNames()) {
const value = pi.getAttribute(name);
console.log(name, value);
}
// logs:
// name placeholder
// more info
Specifications
Browser compatibility
See also
- ProcessingInstruction.hasAttribute()
- ProcessingInstruction.hasAttributes()
- ProcessingInstruction.getAttribute()
- ProcessingInstruction.setAttribute()
- ProcessingInstruction.removeAttribute()
- ProcessingInstruction.toggleAttribute()
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
