关于Raphaël.js清算集绘图的问题

时间:2014-06-19 19:59:08

标签: javascript jquery raphael

请你看一下this sample,让我知道如何在Raphaël上清除一套图纸?

var paper = Raphael('my-canvas', 500, 500);
var rect = paper.rect(100, 100, 100, 100);
var st = paper.set();
st.push(
    paper.circle(10, 10, 5),
    paper.circle(30, 10, 5)
);
st.attr({fill: "red"}); 

$(".clear").on("click",function(){
  st.clear();
});

1 个答案:

答案 0 :(得分:1)

如果要完全删除元素,可能需要使用element.remove()方法。从一组中,您可以在其上应用forEach。所以代码看起来像......

var paper = Raphael('my-canvas', 500, 500);
var rect = paper.rect(100, 100, 100, 100);
var st = paper.set();
st.push(
    paper.circle(10, 10, 5),
    paper.circle(30, 10, 5)
);
st.attr({fill: "red"}); 

$(".clear").on("click",function(){
    st.forEach( function(el) {
        el.remove();
    });

// edit: or shorten it and just have st.remove()
});

jsfiddle