0

После

parser = new DOMParser();
doc = parser.parseFromString(stringContainingSVGSource, "image/svg+xml");
// returns a SVGDocument, which also is a Document.

получил javascript объект #document, как присоединять этот #document к dom элементу ?

После appendChild получаю ошибку

Failed to execute 'appendChild' on 'Node': Nodes of type '#document' may not be inserted inside nodes of type 'DIV'

1 ответ 1

2

SVGDocument.documentElement

const svgString = `
<svg viewBox="0 0 120 120" version="1.1" xmlns="http://www.w3.org/2000/svg">
  <circle cx="30" cy="30" r="30"/>
</svg>`;

const parser = new DOMParser();
const svg = parser.parseFromString(svgString, "image/svg+xml");

document.body.appendChild(svg.documentElement);

В примере выше svg это объект документа, мы не можем вставить в DOM документ, нам нужен дом-узел - Node.

svg.documentElement или svg.rootElement это самый верхний узел.

2
  • Если возможно объясните что именно было не так и как сработал documentElement
    – a_main
    22 сен 2018 в 19:51
  • @a_mine добавил небольшое описание 22 сен 2018 в 20:21

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.