jQuery addClass不处理数组中的元素

时间:2014-07-04 18:10:00

标签: javascript jquery vivagraphjs

我有一个DOM元素数组,我试图迭代它们并使用jQuery添加一个类,但jQuery似乎没有使用addClass方法设置类。但是,attr功能仍然有效。我做错了什么?

以下是我在控制台上的结果。

jQuery addclass not working

http://jsfiddle.net/kf77m/1/

line 64
$(ui).addClass("class", "js-tooltip");

2 个答案:

答案 0 :(得分:0)

查看jquery source我将此行为归因于#8317:

elem.className = finalValue;

看到SVG元素没有这个属性,就不会设置类(或者 - 特定的行仍然会运行,它不会做任何事情)

请参阅评论以获取更多信息!

答案 1 :(得分:0)

由于eithedog打败我发帖,jQuery使用element.className。问题是SVG元素将element.className值视为readonly,这意味着jQuery尝试设置类没有任何作用:

if ( elem.className !== finalValue ) {
    elem.className = finalValue;
}

请参阅https://github.com/jquery/jquery/blob/master/src/attributes/classes.js#L46

RECT个元素实现SVGRectElement接口:

interface SVGRectElement : SVGElement,
                           SVGTests,
                           SVGLangSpace,
                           SVGExternalResourcesRequired,
                           SVGStylable,
                           SVGTransformable {
  readonly attribute SVGAnimatedLength x;
  readonly attribute SVGAnimatedLength y;
  readonly attribute SVGAnimatedLength width;
  readonly attribute SVGAnimatedLength height;
  readonly attribute SVGAnimatedLength rx;
  readonly attribute SVGAnimatedLength ry;
};

http://www.w3.org/TR/SVG/shapes.html#InterfaceSVGRectElement

SVGRectElement实施SVGStylable,其中包含这个小花絮:

className (readonly SVGAnimatedString)

请参阅http://www.w3.org/TR/SVG/types.html#InterfaceSVGStylable

相关问题