此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

ProcessingInstruction:target 属性

Baseline Widely available

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

target 属性是 ProcessingInstruction 接口的一个只读属性,它表示该 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 = `这个处理指令的 target 是:${doc.firstChild.target}`;

在 HTML 文档中

在 HTML 文档中,处理指令行将被视为并表示为 Comment 对象。

html
<?xml version="1.0"?>
<pre></pre>
js
const node = document.querySelector("pre").previousSibling.previousSibling;
const result = `节点的处理指令:${node.nodeName}: ${node.nodeValue}\n`;
document.querySelector("pre").textContent = result;

规范

Specification
DOM
# dom-processinginstruction-target

浏览器兼容性

参见