Attr#

Attr.localName#

Właściwość localName zwraca nazwę lokalną dla danego atrybutu (czyli część lokalną z nazwy kwalifikowanej). Właściwość jest tylko do odczytu.

Opis działania#

Samo wywołanie i poszczególne jego części najlepiej objaśnić na zapisie składniowym:

  1. L
  2. K
  3. T'
  4. T
  5. A
  6. O
  7. Z'
  8. Z
  9. #
var local_name = attr.localName;

gdzie poszczególne człony oznaczają:

Prosty przykład:

  1. L
  2. K
  3. T'
  4. T
  5. A
  6. O
  7. Z'
  8. Z
  9. #
<script>

	var html = document.documentElement; // referencja do elementu HTML
	html.setAttributeNS("www.test.com", "p:AAA", "Test"); // ustawiamy nowy atrybut
	var attr = html.attributes; // pobieramy listę atrybutów
	var firstAttr = attr[0]; // referencja do pierwszego atrybutu z listy

	document.write("Ustawiamy własny atrybut dla elementu HTML:");
	document.write("<br>");
	document.write("html.setAttributeNS('www.test.com', 'p:AAA', 'Test');");

	document.write("<br><br>");

	document.write("Pobieramy listę atrybutów elementu HTML:");
	document.write("<br>");
	document.write("html.attributes: " + attr); // [object NamedNodeMap]
	document.write("<br>");
	document.write("attributes.length: " + attr.length); // 1

	document.write("<br><br>");

	document.write("Odczytujemy właściwości pierwszego atrybutu z listy:");
	document.write("<br>");
	document.write("attributes[0]: " + firstAttr); // [object Attr]
	document.write("<br>");
	document.write("attributes[0].value: " + firstAttr.value); // Test
	document.write("<br>");
	document.write("attributes[0].nodeValue: " + firstAttr.nodeValue); // Test
	document.write("<br>");
	document.write("attributes[0].textContent: " + firstAttr.textContent); // Test
	document.write("<br>");
	document.write("attributes[0].name: " + firstAttr.name); // p:AAA
	document.write("<br>");
	document.write("attributes[0].nodeName: " + firstAttr.nodeName); // p:AAA
	document.write("<br>");
	document.write("attributes[0].localName: " + firstAttr.localName); // AAA
	document.write("<br>");
	document.write("attributes[0].prefix: " + firstAttr.prefix); // p
	document.write("<br>");
	document.write("attributes[0].namespaceURI: " + firstAttr.namespaceURI); // www.test.com
	document.write("<br>");
	document.write("attributes[0].ownerElement: " + firstAttr.ownerElement); // [object HTMLHtmlElement]
	document.write("<br>");
	document.write("attributes[0].specified: " + firstAttr.specified); // true

	document.write("<br><br>");

	document.write(document.localName); // undefined (Firefox, Chrome), null (pozostałe przeglądarki) - błędne działanie
	document.write("<br>");
	document.write(Attr.prototype.hasOwnProperty("localName")); // true
	document.write("<br>");
	document.write(firstAttr.__proto__.hasOwnProperty("localName")); // true
	document.write("<br>");
	document.write(firstAttr.__proto__); // [object AttrPrototype]

</script>

Właściwość localName dla interfejsu Attr pojawia się dopiero w specyfikacji DOM4. W starszych wersjach DOM właściwość była definiowana dla dowolnego węzła, ale obecnie została całkowicie usunięta z interfejsu Node.

Na chwilę obecną jedynie przeglądarki Firefox i Chrome obsługują właściwość localName zgodnie z najnowszymi wymaganiami specyfikacji DOM4, pozostałe programy implementują wcześniejsze wytyczne (szczegóły).

Składnia Web IDL#

  1. L
  2. K
  3. T'
  4. T
  5. A
  6. O
  7. Z'
  8. Z
  9. #
interface Attr {
	readonly attribute DOMString localName;
}

Specyfikacje i inne materiały#

Pasek społecznościowy

SPIS TREŚCI AKTUALNEJ STRONY

Attr (H1) Attr.localName (H2) Opis działania (H3) Składnia Web IDL (H3) Specyfikacje i inne materiały (H3)