Create a <p> element and append it to the document:
const para = document.createElement("p");
para.innerText = "This is a paragraph";
document.body.appendChild(para);
Try it Yourself »
Create a <p> element and append it to an element:
const para = document.createElement("p");
para.innerHTML = "This is a paragraph.";
document.getElementById("myDIV").appendChild(para);
Try it Yourself »
More examples below.
The createElement()
method creates an element node.
document.createElement(
type)
Parameter | Description |
type | Required. The type of element to create. |
Type | Description |
Node | The created element node. |
Create a button:
const btn = document.createElement("button");
btn.innerHTML = "Hello Button";
document.body.appendChild(btn);
Try it Yourself »
document.createElement()
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!