Class Raphael对象上的查询类

Class Raphael对象上的查询类,class,raphael,Class,Raphael,我使用jQuery和Raphael创建了这个矩形数组: squares = []; for (i = 0; i < 2; ++i) { var square = paper.rect(0 + 100*i, 0, 70, 70); square.node.idx = i; square.node.setAttribute('class', 'foo'); squares.push(square); } 或 但是不是: alert(squares[0].att

我使用jQuery和Raphael创建了这个矩形数组:

squares = [];
for (i = 0; i < 2; ++i) {
    var square = paper.rect(0 + 100*i, 0, 70, 70);
    square.node.idx = i;
    square.node.setAttribute('class', 'foo');
    squares.push(square);
}

但是不是

alert(squares[0].attr('class'));
是否有特殊原因导致此项无效? 是否有(其他)方法查询class属性

谢谢, Adrian

SVG中的类与其他任何东西中的类一样,而在Raphael中,处理SVG和IE的VML,事情变得更加棘手

首先,它们位于页面的DOM元素(Raphael的输出)上,而不是Raphael JS对象本身。您可以使用Raphael的
.node
来获取实际的DOM路径(例如,使用jQuery,
$(squares[0].node).someJqueryFunction();
),但由于上述原因,最好尽可能避免这种情况。有更多信息的答案


如果您想使用类来存储数据(例如,使用“活动”、“非活动”类作为开关),最好使用Raphael类,它用于存储任意值。有更多信息的答案。

(刚刚编辑此内容,不建议直接向Raphael对象添加任意数据的不良做法,而是使用.data函数,我听说该函数就是为此而设计的)
alert(squares[0].attr('width'));
alert(squares[0].attr('class'));