简体   繁体   English

为什么setAttribute在jQuery的attr没有的时候工作

[英]why does setAttribute work when jQuery's attr doesn't

I'm trying to dynamically rotate a gradient in an SVG based on mouse position and it's all working fine except this one line. 我正在尝试根据鼠标位置动态旋转SVG中的渐变,除了这一行之外它都工作正常。 The trouble is I can't seem to get the jQuery attr method to work. 麻烦的是我似乎无法让jQuery attr方法起作用。 setAttribute works fine to replace rotate(#,#,#) inside of the attribute gradientTransform but attr doesn't. setAttribute可以很好地替换属性gradientTransform rotate(#,#,#)attr不能。 The part that I'm really having trouble with is that I can change other attributes with attr (as in the third example in the fiddle). 我真正遇到麻烦的部分是我可以用attr改变其他属性(如小提琴中的第三个例子)。

http://jsfiddle.net/samt/8yUNL/ http://jsfiddle.net/samt/8yUNL/

this one works 这个有效

mainLogoFill[0].setAttribute('gradientTransform', 'rotate(45,100,100)');

this one doesn't 这一个没有

mainLogoFill.attr('gradientTransform', 'rotate(90,100,100)');

this one throws me.. why does it work? 这一个让我...为什么它有效?

mainLogoFill.attr('x1', '100');

jQuery uses toLowerCase with attribute names, so it ends up as gradienttransform not gradientTransform , which is a new attribute and not the same as the one you already have, hence the issues. jQuery使用带有属性名称的toLowerCase,因此它最终为gradienttransform而不是gradientTransform ,这是一个新属性,与您已有的属性不同,因此存在问题。

The attr() method starts like this attr()方法就像这样开始

function (elem, name, value) {
    var hooks, ret, nType = elem.nodeType;

    // don't get/set attributes on text, comment and attribute nodes
    if (!elem || nType === 3 || nType === 8 || nType === 2) {
        return;
    }

    // Fallback to prop when attributes are not supported
    if (typeof elem.getAttribute === core_strundefined) {
        return jQuery.prop(elem, name, value);
    }

    // All attributes are lowercase
    // Grab necessary hook if one is defined
    if (nType !== 1 || !jQuery.isXMLDoc(elem)) {
        name = name.toLowerCase(); // this is the line where the name is lowercased
        hooks = jQuery.attrHooks[name] || (jQuery.expr.match.bool.test(name) ? boolHook : nodeHook);
    }

    if (value !== undefined) {

The way around this is to use setAttribute instead. 解决这个问题的方法是使用setAttribute。

Although SVG is based on XML (and thus uses case sensitive attribute names), jQuery's detection fails at this point and transforms attribute names to lower case. 虽然SVG基于XML(因此使用区分大小写的属性名称),但此时jQuery的检测失败并将属性名称转换为小写。

If you don't like using .setAttribute() everywhere, an alternative is to patch jQuery by overriding how it determines XML mode: 如果您不喜欢在任何地方使用.setAttribute() ,则可以通过覆盖jQuery如何确定XML模式来修补jQuery:

jQuery.isXMLDoc = function(elem)
{
    return (elem.ownerDocument || elem).documentElement.nodeName !== "HTML" || 
        (elem.namespaceURI).indexOf('html') == -1;
}

The above code additionally checks if the namespace of the current element has the term "html" in it; 上面的代码还检查当前元素的名称空间中是否包含术语"html" ; the namespace for SVG is "http://www.w3.org/2000/svg" , so jQuery will not alter the case of your property names. SVG的命名空间是"http://www.w3.org/2000/svg" ,因此jQuery不会改变您的属性名称的大小写。

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM