ProcessingInstruction: target 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

ProcessingInstruction 介面的只讀 target 屬性代表 ProcessingInstruction 所針對的應用程式。

例如

html
<?xml version="1.0"?>

是一個處理指令,其 targetxml

一個包含應用程式名稱的字串。

示例

在 XML 文件中

js
let parser = new DOMParser();
const doc = parser.parseFromString(
  '<?xml version="1.0"?><test/>',
  "application/xml",
);
const pi = doc.createProcessingInstruction(
  "xml-stylesheet",
  'href="mycss.css" type="text/css"',
);
doc.insertBefore(pi, doc.firstChild);

const output = document.querySelector("output");
output.textContent = `This processing instruction's target is: ${doc.firstChild.target}`;

在 HTML 文件中

處理指令行將被視為並表示為一個 Comment 物件。

html
<?xml version="1.0"?>
<pre></pre>
js
const node = document.querySelector("pre").previousSibling.previousSibling;
const result = `Node with the processing instruction: ${node.nodeName}: ${node.nodeValue}\n`;
document.querySelector("pre").textContent = result;

規範

規範
DOM
# dom-processinginstruction-target

瀏覽器相容性

另見

  • DOM API