Document: phương thức createProcessingInstruction()
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.
createProcessingInstruction() tạo một nút processing instruction mới và trả về nó.
Nút mới thường sẽ được chèn vào một tài liệu XML để đạt được bất kỳ điều gì với nó, chẳng hạn như với node.insertBefore.
Cú pháp
js
createProcessingInstruction(target, data)
Tham số
piNode-
Nút
ProcessingInstructionkết quả. target-
Một chuỗi chứa phần đầu tiên của processing instruction (ví dụ:
<?target … ?>) data-
Một chuỗi chứa bất kỳ thông tin nào mà processing instruction cần mang, sau target. Dữ liệu tùy thuộc vào bạn, nhưng nó không được chứa
?>, vì điều đó sẽ đóng processing instruction.
Giá trị trả về
Không có (undefined).
Ngoại lệ
InvalidCharacterErrorDOMException-
Được ném ra nếu một trong các điều kiện sau đúng:
Ví dụ
js
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/>
Đặc tả kỹ thuật
| Specification |
|---|
| DOM> # ref-for-dom-document-createprocessinginstruction①> |