克隆 svg 组
我有一个 svg 组,其中包含一些元素,我想克隆该组,问题是该函数仅克隆该组的一个元素。 这是函数
<script type="text/ecmascript"><![CDATA[
function clone(evt) {
var cloneElement = evt.target.cloneNode(false);
var newx = 100;
var newy = 500;
cloneElement.setAttributeNS(null,"x",newx);
cloneElement.setAttributeNS(null,"y",newy);
document.getElementById("layer1").appendChild(cloneElement);
}
]]></script>
svg 看起来像
<g id="layer1" onclick="clone(evt)">
<rect>
<path>
<circle>
<circle>
</g>
矩形就像一个容器,发生的事情是该函数克隆矩形并保留其他元素。 那么有什么问题吗?
I have an svg group which contains some elements, I want to clone the group, problem is the function clones only one element of the group.
Here is the function
<script type="text/ecmascript"><![CDATA[
function clone(evt) {
var cloneElement = evt.target.cloneNode(false);
var newx = 100;
var newy = 500;
cloneElement.setAttributeNS(null,"x",newx);
cloneElement.setAttributeNS(null,"y",newy);
document.getElementById("layer1").appendChild(cloneElement);
}
]]></script>
The svg looks something like
<g id="layer1" onclick="clone(evt)">
<rect>
<path>
<circle>
<circle>
</g>
The rectangle is like a container and what happens is that the function clones the rectangle and leaves the other elements.
So what is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两件事:
cloneNode
应该传递true,否则它只会克隆一个元素evt.target
将始终是事件起源的元素, g 元素永远不会被直接击中,鼠标事件只是从子元素中冒泡到那里。如果您想要当前正在处理事件的元素(在您的情况下是 g 元素),您可以使用 evt.currentTarget 来代替。Two things:
cloneNode
should be passed true if you want a deeply cloned tree, otherwise it will just clone one elementevt.target
will always be the element where the event originated, and g elements are never hit directly, the mouse events just bubble up to there from the children. You can useevt.currentTarget
instead if you want the element which is currently handling the event (in your case that would be the g element).