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