Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
Cómo agregar un div dentro de svg con javascript

Estoy tratando de averiguar cómo puedo usar javascript para generar el siguiente svg programación.

 <svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"> <rect class="bg" id="bg" x="0" y="0" width="200" height="200" fill="#0357D5"></rect> <foreignObject x="0" y="0" width="60" height="50"> <div xmlns="http://www.w3.org/1999/xhtml">Thanks </div> </foreignObject> </svg>

Esto es lo que probé

 const svg = document.querySelector("svg"); const svgns = "http://www.w3.org/2000/svg"; let bg = document.createElementNS(svgns, 'rect'); bg.setAttribute('class', 'bg'); bg.setAttribute('id', 'bg'); bg.setAttribute('x',"0"); bg.setAttribute("y","0"); bg.setAttribute("width", "200"); bg.setAttribute("height", '200'); bg.setAttribute("fill","#0357D5"); svg.appendChild(bg); let fo = document.createElementNS(svgns, 'foreignObject'); fo.setAttribute("x", "0"); fo.setAttribute("y", "0"); fo.setAttribute("width", "60"); fo.setAttribute("height", "50"); svg.appendChild(fo); let _div = document.createElementNS(svgns, 'div'); _div.setAttribute("xmlns", 'http://www.w3.org/1999/xhtml'); _div.textContent = 'Thanks'; svg.appendChild(_div); fo.appendChild(_div);
 <svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"> </svg>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Debe usar createElement('div') , no createElementNS , ya que no es un elemento svg.

 const svg = document.querySelector("#svg"); const svgns = "http://www.w3.org/2000/svg"; let bg = document.createElementNS(svgns, 'rect'); bg.setAttribute('class', 'bg'); bg.setAttribute('id', 'bg'); bg.setAttribute('x',"0"); bg.setAttribute("y","0"); bg.setAttribute("width", "200"); bg.setAttribute("height", '200'); bg.setAttribute("fill","#0357D5"); svg.appendChild(bg); let fo = document.createElementNS(svgns, 'foreignObject'); fo.setAttribute("x", "0"); fo.setAttribute("y", "0"); fo.setAttribute("width", "60"); fo.setAttribute("height", "50"); svg.appendChild(fo); let _div = document.createElement('div'); _div.setAttribute("xmlns", 'http://www.w3.org/1999/xhtml'); _div.textContent = 'Thanks'; svg.appendChild(_div); fo.appendChild(_div);
 svg{ display:inline-block; width:20em; }
 <svg id="svg" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"> </svg>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda