0

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

这是我从控制台得到的结果。

jQuery addclass 不起作用

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

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

2 回答 2

0

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

elem.className = finalValue;

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

有关更多信息,请参阅评论!

于 2014-07-04T18:49:52.167 回答
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

于 2014-07-04T18:57:41.773 回答