Document: createProcessingInstruction() method


目录

{{APIRef("DOM")}}

The createProcessingInstruction() method of the {{domxref("Document")}} interface creates a new {{domxref("ProcessingInstruction")}} object and returns it.

Syntax

createProcessingInstruction(target, data)

Parameters

Return value

Exceptions

Description

The createProcessingInstruction() method creates a new processing instruction. The new node will usually be inserted into a document to accomplish a task with it, using a method such as {{ domxref("node.insertBefore") }}.

Initially, ProcessingInstruction nodes were only supported in XML documents, not in HTML documents. In non-supporting browsers, a processing instruction will be interpreted as a comment and represented as a {{domxref("Comment")}} object in the DOM tree.

Examples

Basic usage

This example creates an <xml-stylesheet> processing instruction and adds it to the top of an example XML document.

const doc = new DOMParser().parseFromString("<foo />", "application/xml");
const pi = doc.createProcessingInstruction(
  "xml-stylesheet",
  'href="mycss.css"',
);

doc.insertBefore(pi, doc.firstChild);

console.log(new XMLSerializer().serializeToString(doc));
// Displays: <?xml-stylesheet href="mycss.css" type="text/css"?><foo/>

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}


版本号 #2
由 壳壳虫 创建于 2026-10-31 03:27:53 UTC
由 壳壳虫 更新于 2026-10-31 03:27:53 UTC