zoukankan      html  css  js  c++  java
  • 使用jquery 1.7 及以后的版本 attr 问题

    跟进jquery的代码进行检查,发现问题出在下面的代码中:

    if ( notxml ) {
                name = name.toLowerCase();
                hooks = jQuery.attrHooks[ name ] || ( rboolean.test( name ) ? boolHook : nodeHook );
            }

    此处将属性名变成了全小写,导致dom对象内置的属性无法取到值。

    代码所在位置第2298行,

    attr函数完整代码:

    复制代码
    attr: function( elem, name, value, pass ) {
            var ret, hooks, notxml,
                nType = elem.nodeType;
    
            // don't get/set attributes on text, comment and attribute nodes
            if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
                return;
            }
    
            if ( pass && jQuery.isFunction( jQuery.fn[ name ] ) ) {
                return jQuery( elem )[ name ]( value );
            }
    
            // Fallback to prop when attributes are not supported
            if ( typeof elem.getAttribute === "undefined" ) {
                return jQuery.prop( elem, name, value );
            }
    
            notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
    
            // All attributes are lowercase
            // Grab necessary hook if one is defined
            if ( notxml ) {
                name = name.toLowerCase();
                hooks = jQuery.attrHooks[ name ] || ( rboolean.test( name ) ? boolHook : nodeHook );
            }
    
            if ( value !== undefined ) {
    
                if ( value === null ) {
                    jQuery.removeAttr( elem, name );
                    return;
    
                } else if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value, name )) !== undefined ) {
                    return ret;
    
                } else {
                    elem.setAttribute( name, "" + value );
                    return value;
                }
    
            } else if ( hooks && "get" in hooks && notxml && (ret = hooks.get( elem, name )) !== null ) {
                return ret;
    
            } else {
    
                ret = elem.getAttribute( name );//此处取属性的方法对属性名称是大小写敏感的
    
                // Non-existent attributes return null, we normalize to undefined
                return ret === null ?
                    undefined :
                    ret;
            }
    复制代码

    测试环境 IE,FF

  • 相关阅读:
    Email:2017
    mac下使用QuickTime录屏及上传youku注意事项
    unity, 自定义类中使用print
    unity, 集成iOS广告sdk注意事项
    xcode,不要将.a文件拖到xcode里
    unity, 慎用DontDestroyOnLoad
    unity, 弹出panel一定要放在UI Hierarchy的底端
    unity, iOS下画面错乱解法
    (转)NSString to string(支持中文)
    objective-c的观察者模式
  • 原文地址:https://www.cnblogs.com/opps/p/4011735.html
Copyright © 2011-2022 走看看