Phương thức HTML DOM insertAdjacentText ()
Thí dụ
Chèn văn bản sau phần tử tiêu đề được chỉ định:
var h = document.getElementById("myH2");
h.insertAdjacentText("afterend",
"My inserted text");
Định nghĩa và Cách sử dụng
Phương insertAdjacentText()
thức này sẽ chèn một phần tử được chỉ định vào một vị trí được chỉ định.
Giá trị vị trí pháp lý là:
"afterbegin"
"afterend"
"beforebegin"
"beforeend"
Hỗ trợ trình duyệt
Các số trong bảng chỉ định phiên bản trình duyệt đầu tiên hỗ trợ đầy đủ phương pháp này.
Method | |||||
---|---|---|---|---|---|
insertAdjacentText() | Yes | Yes | 48 | Yes | Yes |
Cú pháp
node.insertAdjacentText(position, text)
Giá trị tham số
Parameter | Type | Description |
---|---|---|
position | String | Required. A position relative to the element. Legal values: "afterbegin" - After the beginning of the element (as the first child) "afterend" - After the element "beforebegin" - Before the element "beforeend" - Before the end of the element (as the last child) |
text | String | The text you want to insert |
Các ví dụ khác
Thí dụ
Sử dụng giá trị "afterbegin":
var h = document.getElementById("myH2");
h.insertAdjacentText("afterbegin",
"My inserted text");
Thí dụ
Sử dụng giá trị "beforebegin":
var h = document.getElementById("myH2");
h.insertAdjacentText("beforebegin",
"My inserted text");
Thí dụ
Sử dụng giá trị "beforeend":
var h = document.getElementById("myH2");
h.insertAdjacentText("beforeend",
"My inserted text");