Js добавить html append

Метод append

Метод append позволяет вставить в конец какого-либо элемента другой элемент. Параметром метод принимает элемент, как правило созданный через createElement , либо строку. Можно добавить сразу несколько элементов или строк, перечислив их через запятую.

Синтаксис

Пример

Давайте создадим абзац, установим ему текст и поместим на страницу в конец блока #parent :

1

2

3

let parent = document.querySelector(‘#parent’); let p = document.createElement(‘p’); p.textContent = ‘!’; parent.append(p);

Результат выполнения кода:

Пример

Поместим сразу несколько абзацев в конец блока #parent :

1

2

3

let parent = document.querySelector(‘#parent’); let p1 = document.createElement(‘p’); p1.textContent = ‘a’; let p2 = document.createElement(‘p’); p2.textContent = ‘b’; parent.append(p1, p2);

Результат выполнения кода:

Пример

Давайте в качестве параметра метода используем строку:

1

2

3

let parent = document.querySelector(‘#parent’); parent.append(‘!’);

Результат выполнения кода:

Пример

Дан ul . Давайте разместим в нем 9 тегов li , при этом их текстом сделаем порядковые номера:

Результат выполнения кода:

Пример

Давайте заполним таблицу tr-ками и td-шками:

Результат выполнения кода:

Смотрите также

  • метод prepend ,
    который вставляет элементы в начало
  • метод appendChild ,
    который вставляет элементы в конец
  • метод insertBefore ,
    который вставляет элемент в перед элементом
  • метод insertAdjacentElement ,
    который вставляет элемент в заданное место
  • метод insertAdjacentHTML ,
    который вставляет теги в заданное место
Читайте также:  Doiso ru login index php

Источник

Document: append() method

The Document.append() method inserts a set of Node objects or string objects after the last child of the document. String objects are inserted as equivalent Text nodes.

This method appends a child to a Document . To append to an arbitrary element in the tree, see Element.append() .

Syntax

append(param1) append(param1, param2) append(param1, param2, /* … ,*/ paramN) 

Parameters

A set of Node or string objects to insert.

Return value

Exceptions

Thrown when the node cannot be inserted at the specified point in the hierarchy.

Examples

Appending a root element to a document

If you try to append an element to an existing HTML document, it might throw a HierarchyRequestError DOMException given a element already exists.

let html = document.createElement("html"); document.append(html); // HierarchyRequestError: The operation would yield an incorrect node tree. 

If you are creating a new document without any existing element, you can append a root HTML element (or a root SVG element):

let doc = new Document(); let html = document.createElement("html"); doc.append(html); doc.children; // HTMLCollection [] 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Apr 7, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

Element: append() method

The Element.append() method inserts a set of Node objects or string objects after the last child of the Element . String objects are inserted as equivalent Text nodes.

  • Element.append() allows you to also append string objects, whereas Node.appendChild() only accepts Node objects.
  • Element.append() has no return value, whereas Node.appendChild() returns the appended Node object.
  • Element.append() can append several nodes and strings, whereas Node.appendChild() can only append one node.
Читайте также:  background-repeat

Syntax

append(param1) append(param1, param2) append(param1, param2, /* … ,*/ paramN) 

Parameters

A set of Node or string objects to insert.

Return value

Exceptions

Thrown when the node cannot be inserted at the specified point in the hierarchy.

Examples

Appending an element

let div = document.createElement("div"); let p = document.createElement("p"); div.append(p); console.log(div.childNodes); // NodeList [ 

]

Appending text

let div = document.createElement("div"); div.append("Some text"); console.log(div.textContent); // "Some text" 

Appending an element and text

let div = document.createElement("div"); let p = document.createElement("p"); div.append("Some text", p); console.log(div.childNodes); // NodeList [ #text "Some text", 

]

The append method is unscopable

The append() method is not scoped into the with statement. See Symbol.unscopables for more information.

let div = document.createElement("div"); with (div)  append("foo"); > // ReferenceError: append is not defined 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Apr 7, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

Оцените статью