问题描述
|
我有使用jQuery和Raphael的这些rect数组:
squares = [];
for (i = 0; i < 2; ++i) {
var square = paper.rect(0 + 100*i,70,70);
square.node.idx = i;
square.node.setAttribute(\'class\',\'foo\');
squares.push(square);
}
我可以成功查询各种属性,例如:
alert(squares[0].attr(\'x\'));
要么
alert(squares[0].attr(\'width\'));
但不是:
alert(squares[0].attr(\'class\'));
是否有特殊原因对此无效?
有(其他)查询类属性的方法吗?
谢谢,
阿德里安
解决方法
SVG中的类与其他所有类都不完全相同-在涉及SVG和IE \ VML的Raphael中,事情变得更加繁琐。
首先,它们位于页面的DOM元素(Raphael的输出)上,而不位于Raphael JS对象本身中。您可能会使用Raphael的
.node
来获取实际的DOM路径(例如,使用jQuery,$(squares[0].node).someJqueryFunction();
),但是由于上述原因,最好避免这种情况。这个相关问题的答案与更多信息有关。
如果要使用类来存储数据(例如,使用\'active \',\'inactive \'类作为开关),最好使用Raphael的.data
函数,该函数显然用于存储任意值。这个相关问题的答案与更多信息有关。