Chèn phần tử DOM HTMLAdjacentText()
Ví dụ
Chèn văn bản sau phần tử tiêu đề:
const h2 = document.getElementById("myH2");
let text = "My inserted text";
h2.insertAdjacentText("afterend", text);
Hãy tự mình thử »Sự miêu tả
Phương thức insertAdjacentText()
chèn văn bản vào một vị trí được chỉ định.
Vị trí pháp lý:
Giá trị | Sự miêu tả |
---|---|
sau khi bắt đầu | Sau phần đầu của phần tử (con đầu tiên) |
sau cùng | Sau phần tử |
trước khi bắt đầu | Trước phần tử |
trước khi kết thúc | Trước phần tử kết thúc (con cuối cùng) |
Cú pháp
element .insertAdjacentText( position, text )
hoặc
node .insertAdjacentText( position, text )
Thông số
Parameter | Description |
position | Required. A position relative to the element: afterbegin afterend beforebegin beforeend |
text | The text to insert. |
Thêm ví dụ
Ví dụ
Sử dụng "sau khi bắt đầu":
const h2 = document.getElementById("myH2");
let text = "My inserted text";
h2.insertAdjacentText("afterbegin", text);
Hãy tự mình thử »Hỗ trợ trình duyệt
element.insertAjacentText()
được hỗ trợ trong tất cả các trình duyệt:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |