如何使用cloneNode& svg脚本中的appendChild方法?

时间:2015-12-14 04:50:36

标签: javascript dom svg

我的问题是如何在svg脚本中使用Node Object的方法? 我搜索了How do I use javascript to clone an SVG element?Trying to move one option to another select list这样的答案,但它对我不起作用。我想在一个组中加入两个svg元素列表。这是我的代码:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<svg version="1.1" id="my_svg" viewBox="0 0 1440 900">
<g id="blocks">
<rect x="941.616" y="35.935" width="26.5" height="26.5"/>
<polygon points="936.717,62.391 936.717,62.391 936.717,62.391 936.717,35.934 910.261,35.934 910.261,35.935 
910.261,35.935 910.26,35.935 910.26,62.391 910.261,62.391 936.717,62.391 "/>
<rect x="741.162" y="120.203" width="26.5" height="26.5"/>
<polygon points="736.263,146.66 736.263,146.659 736.263,146.659 736.263,120.203 709.807,120.203 709.807,120.203 
709.806,120.203 709.806,120.203 709.806,146.66 709.806,146.66 736.263,146.66 "/>
...
</g>
</svg>
<script type="text/javascript">
function myFunction(){
var list_a = [];
var new_list = document.getElementById("blocks");
var rects = new_list.getElementsByTagName("rect");
var polygons = new_list.getElementsByTagName("polygon");
var length = rects.length + polygons.length;
list_a = rects.cloneNode(true);
polygons.appendChild(list_a);
console.log(list_a.length);
}
</script>
</body>
</html>

0 个答案:

没有答案