简体   繁体   English

插入一个 <g> 通过jQuery在SVG中标记

[英]Intercalate a <g> tag in SVG thru jquery

I try to intercalate a element in my SVG to add a layer in my SVG DOM. 我尝试在SVG中插入元素以在SVG DOM中添加一个层。 The SVG is embeded in a HTML5. SVG嵌入HTML5中。

When I do this, the moved element disapears from the HTML. 当我这样做时,已移动的元素将从HTML中消失。 what's wrong? 怎么了?

To see it live, have a look here: http://bl.ocks.org/daohodac/raw/6db306dcb5d66d913f5c/ and click the button to perform the script 要实时观看,请在这里查看: http : //bl.ocks.org/daohodac/raw/6db306dcb5d66d913f5c/并单击按钮以执行脚本

here is the initial SVG with the polygon visible: 这是可见的多边形的初始SVG:

<svg ...attributes...>
  <g id="zoom_anim" ...attributes...>
    <polygon ...attributes...></polygon>
  </g>
</svg>

here is what I have after the script (I can see it in chrome inspector) with the polygon invisible 这是在多边形不可见的脚本(我可以在chrome inspector中看到)之后的内容

<svg ...attributes...>
  <g id="zoom_anim_parent_bbsmashed">
    <g id="zoom_anim" ...attributes...>
      <polygon ...attributes...></polygon>
    </g>

</svg>

and here is the script 这是脚本

<script>
 var intercalate = function() {
  var zoomParentId = "zoom_anim_parent_bbsmashed";
  var gId = "#zoom_anim";
  $(gId).parent().append($("<g id='"+zoomParentId+"'>"));
  var that = $(gId).detach().appendTo("#"+zoomParentId);
 };
</script>

Be careful using jQuery with SVG. 将jQuery与SVG一起使用时要小心。 SVG nodes are not the same as XHTML nodes, and some of the jQuery API doesn't work correctly with SVG. SVG节点与XHTML节点不同,并且某些jQuery API在SVG上无法正常工作。 To create an SVG node, you should use document.createElementNS('http://www.w3.org/2000/svg', 'g') . 要创建SVG节点,应使用document.createElementNS('http://www.w3.org/2000/svg', 'g') You could also use an SVG library like d3 or snap ; 您也可以使用d3snap之类的SVG库; both offer a comprehensive API to work with SVG nodes. 两者都提供了用于SVG节点的全面API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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