0

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

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..    
    } )

样品工作在这里

4

1 回答 1

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/

于 2012-11-06T13:26:02.660 回答