简体   繁体   中英

Insert SVG elements into HTML tag with jQuery

I have two SVG elements, my markup:

<svg>
  <circle r="260" fill="#1ABCb6" class="" id="svg_el_obj"></circle>
  <circle r="260" fill="#1ABCb6" class="" id="svg_el_obj"></circle>
</svg>

i need to insert them into <g> tag and in output watch this:

<svg>
  <g>
     <circle r="260" fill="#1ABCb6" class="" id="svg_el_obj"></circle>
  </g>
  <g>
     <circle r="260" fill="#1ABCb6" class="" id="svg_el_obj"></circle>
  </g>
</svg>

I try with jQuery but something is not right:

$('#svg_el_obj').each(function(){
   $(this).html('<g></g>');
}

or

$('#svg_el_obj').each(function(){
   $(this).append('<g></g>');
}

Much thx for help.

You have multiple tags with the same id, id are to be unique. Try using a class instead and wrap

$('.svg_el_obj').wrap('<g>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM