拉斐尔在分配数据后,我无法得到它

时间:2012-11-06 11:07:31

标签: jquery raphael

我通过循环分配数据值,但是当我检查每个元素时,没有找到属性...任何一个正确的我的函数?

var paper = new Raphael('slideContainer', 930, 420);

    //mainpage circles;-
var mainPageCircle = {
    circles :{
        cR:{color:'#730000',text:'Content'},
        cB:{color:'#004f74',text:'Consulting'},
        cG:{color:'#146c00',text:'Commerce'}
    },
    radious:100,
    stroke:10,
    strokeColor:'#fff',
    opacity:0.7
}


 $.each(mainCrProp.circles, function (n) {
        pCircles =  paper.circle(
                                     n==='cR' ? (paperWidth/2) - mainCrProp.radious/2 :
                                     n==='cB' ? paper.getById('cR').attr('cx') + mainCrProp.radious : (paperWidth/2),
                                     n==='cR' ? (paperHeight/2) - mainCrProp.radious/2 :
                                     n==='cB' ? paper.getById('cR').attr('cy') : 
                                     n==='cG' ? paper.getById('cR').attr('cy') + mainCrProp.radious : paperHeight/2,
                                     mainCrProp.radious)
        .attr({fill:this.color,'stroke-width':mainCrProp.stroke,stroke:mainCrProp.strokeColor,opacity:mainCrProp.opacity})
        .data('dClass',(i+'Group'))//i am setting my data..
        .id = n;
        //
        pText = paper.text(          n==='cR' ? (paperWidth/2) - mainCrProp.radious:
                                     n==='cB' ? paper.getById('cR').attr('cx') + mainCrProp.radious*1.5 : (paperWidth/2),
                                     n==='cR' ? (paperHeight/2) - mainCrProp.radious/2 :
                                     n==='cB' ? paper.getById('cR').attr('cy') : 
                                     n==='cG' ? paper.getById('cR').attr('cy') + mainCrProp.radious*1.5 : paperHeight/2,
                                     this.text)
        .data('dClass',(i+'Group'))//i am setting my data
        .id = n+'text';
        pGroup[i] = paper.set(pCircles,pText);
        i++;
    } )

    paper.forEach(function (el) {
        console.log(el.data('dClass'));// i  am not getting any data value..    
    } )

Sample work is here

1 个答案:

答案 0 :(得分:1)

首先,id为您提供了Raphael,所以不要覆盖它。这就是为什么你丢失data我认为,看看这个阅读id并设置一些user data的例子:

var paper = new Raphael('container',500,500);

for (var i = 0; i < 5; i++) {
paper.circle(10 + 25 * i, 10, 10)
     .attr({fill: "#000"})
    .data("myData", {customId: "customId:" + i})
     .click(function () {
        console.log(this.data("myData").customId);
 });
}

此处示例 - http://jsfiddle.net/h6U87/3/