SVG createElementNS 'use' 是可能的

SVG createElementNS 'use' is it possible?

本文关键字:SVG use createElementNS      更新时间:2023-09-26

缩短,我正在使用(使用)来复制代码添加到DOM的正方形(路径),但是只有1px(调试铬)。对第六个块用户不可见!谢谢!代码输入: 代码笔

$(function() {
  $('#svg').css('visibility', 'visible');   
  $('#svg use').css('fill', '#fff');
  function addAnim() {
    var $first = $('#svg use:not(.anim):first');
    $first.attr('class', 'anim').css('fill', '#F15A29');    
    $first.animate({fill : '#F15A29'}, 500);
    setTimeout(function() {
      addAnim();
    }, 100);
  }
  function replicar(){
      var svg = document.getElementById('svg'); 
      var elemento= document.createElementNS('ttp://www.w3.org/2000/svg', 'use');
      elemento.setAttribute('xlink:href', '#shape');
      elemento.setAttribute('y', '62');
      elemento.setAttribute('x', '124');
      elemento.setAttribute('fill', '#3D6EB5');
      svg.appendChild(elemento);   

  addAnim();
  replicar(); 
});

您在这一行中缺少一个"h":

var elemento= document.createElementNS('http://www.w3.org/2000/svg', 'use');

此外,xlink 属性需要与 setAttributeNS 一起添加。

elemento.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', '#shape');