Node: lookupPrefix()-Methode

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Die lookupPrefix()-Methode der Node-Schnittstelle gibt einen String zurück, der das Präfix für einen gegebenen Namespace-URI enthält, sofern vorhanden, und null, wenn nicht. Wenn mehrere Präfixe möglich sind, wird das erste Präfix zurückgegeben.

Syntax

js
lookupPrefix(namespace)

Parameter

namespace

Ein String, der den Namespace enthält, für den das Präfix gesucht wird.

Hinweis: Dieser Parameter ist nicht optional, kann aber auf null gesetzt werden.

Rückgabewert

Ein String, der das entsprechende Präfix enthält, oder null, wenn keines gefunden wurde. Wenn namespace null oder der leere String ist, gibt lookupPrefix() null zurück.

Wenn der Knoten ein DocumentType oder ein DocumentFragment ist, gibt lookupPrefix() immer null zurück.

Beispiel

html
Prefix for <code>http://www.w3.org/2000/svg</code> on &lt;output&gt;:
<output>Not tested</output>.<br />
Prefix for <code>http://www.w3.org/XML/1998/namespace</code> on &lt;output&gt;:
<output>Not tested</output>.<br />
Prefix for <code>http://www.w3.org/TR/html4/</code> on &lt;output&gt;:
<output>Not tested</output>.<br />
Prefix for <code>https://www.w3.org/1999/xlink</code> on &lt;output&gt;:
<output>Not tested</output>.<br />
Prefix for <code>http://www.w3.org/2000/svg</code> on &lt;svg&gt;:
<output>Not tested</output>.<br />
Prefix for <code>https://www.w3.org/1999/xlink</code> on &lt;svg&gt;:
<output>Not tested</output>.<br />
Prefix for <code>http://www.w3.org/XML/1998/namespace</code> on &lt;svg&gt;:
<output>Not tested</output>.<br />
<svg xmlns:t="http://www.w3.org/2000/svg" height="1"></svg>
<button>Click to see the results</button>
js
const button = document.querySelector("button");
button.addEventListener("click", () => {
  const aHtmlElt = document.querySelector("output");
  const aSvgElt = document.querySelector("svg");

  const result = document.getElementsByTagName("output");
  result[0].value = aHtmlElt.lookupPrefix("http://www.w3.org/2000/svg"); // true
  result[1].value = aHtmlElt.lookupPrefix(
    "http://www.w3.org/XML/1998/namespace",
  ); // false
  result[2].value = aHtmlElt.lookupPrefix("http://www.w3.org/TR/html4/"); // true
  result[3].value = aHtmlElt.lookupPrefix("https://www.w3.org/1999/xlink"); // false
  result[4].value = aSvgElt.lookupPrefix("http://www.w3.org/2000/svg"); // true
  result[5].value = aSvgElt.lookupPrefix("https://www.w3.org/1999/xlink"); // true
  result[6].value = aSvgElt.lookupPrefix(
    "http://www.w3.org/XML/1998/namespace",
  ); // false
});

Spezifikationen

Specification
DOM
# dom-node-lookupprefix

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
lookupPrefix

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch