【问题标题】:Querying class on Raphael object在 Raphael 对象上查询类
【发布时间】:2013-01-10 04:37:13
【问题描述】:

我有这个使用 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('x'));

alert(squares[0].attr('width'));

不是

alert(squares[0].attr('class'));

是否有特殊原因导致此无效? 是否有(其他)方法来查询类属性?

谢谢, 阿德里安

【问题讨论】:

    标签: class raphael


    【解决方案1】:

    SVG aren't quite the same 中的类作为其他一切中的类 - 而在处理 SVG 和 IE 的 VML 的 Raphael 中,事情变得更加棘手。

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

    如果您想使用类来存储数据(例如,像使用“活动”、“非活动”类作为开关),您最好使用 Raphael 的 .data function,其中 apparently 用于存储任意值。 This related question 有更多信息的答案。

    【讨论】:

    • (只是编辑了这个以不建议直接向 Raphael 对象添加任意数据的不良做法,而是使用我听说为此设计的 .data 函数)
    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多