如何编辑 jquery 函数 [英] How to edit a jquery function

查看:20
本文介绍了如何编辑 jquery 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改 jQuery (V.6.1) 核心中的attr"函数.

I'm trying to modify the 'attr' function in the jQuery (V.6.1) core.

我有一个 plugins.js 文件,它包含在 jquery-6.1.js 文件之后的页面中.plugins.js 文件包含对各种 jQuery 核心函数的改进,以适应一些 SVG 功能.

I have a plugins.js file that's included in the page after the jquery-6.1.js file. The plugins.js file contains improvments made to various jQuery core functions to accomodate some SVG functionality.

我已将 attr 函数复制到 plugins.js 文件中,但并未对其进行修改,但现在在调用时会产生 JavaScript 错误.

I've copied the attr function into the plugins.js file without modifying it, but it now produces a JavaScript error when it's called.

有谁知道为什么这个特定的函数不喜欢被覆盖(我可能以错误的方式覆盖它):

Does anyone know why this particular function doesn't like to be overwritten (I'm probably overwriting it in the wrong fashion):

(function($){

    $.fn.attr = function( elem, name, value, pass ){

        var nType = elem.nodeType;

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

        if ( pass && name in jQuery.attrFn ) {
            return jQuery( elem )[ name ]( value );
        }

        // Fallback to prop when attributes are not supported
        if ( !("getAttribute" in elem) ) {
            return jQuery.prop( elem, name, value );
        }

        var ret, hooks,
            notxml = nType !== 1 || !jQuery.isXMLDoc( elem );

        // Normalize the name if needed
        name = notxml && jQuery.attrFix[ name ] || name;

        hooks = jQuery.attrHooks[ name ];

        if ( !hooks ) {

            // Use boolHook for boolean attributes
            if ( rboolean.test( name ) &&
                (typeof value === "boolean" || value === undefined || value.toLowerCase() === name.toLowerCase()) ) {
                hooks = boolHook;

            // Use formHook for forms and if the name contains certain characters
            } else if ( formHook && (jQuery.nodeName( elem, "form" ) || rinvalidChar.test( name )) ) {
                hooks = formHook;
            }

        }

        if ( value !== undefined ) {

            if ( value === null ) {
                jQuery.removeAttr( elem, name );
                return undefined;

            } 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 ) {

            return hooks.get( elem, name );

        } else {

            ret = elem.getAttribute( name );

            // Non-existent attributes return null, we normalize to undefined
            return ret === null ?
                undefined :
                ret;
        }

    };

})(jQuery);

推荐答案

与其完全覆盖基本的 attr 函数,不如像这样扩展它:

Instead of completely overwriting the base attr function, just extend it like this:

(function($){
    var jqAttr = $.fn.attr;
    $.fn.attr = function( elem, name, value, pass ) {
        // check to see if it's the special case you're looking for
        if (name === 'transform') {
            // handle your special SVG case here
        } else {
            jqAttr(elem, name, value, pass);
        }
    };
}(jQuery));

我做了类似的事情来覆盖 $.each 以提供一些自定义功能.我不确定这是否会修复您遇到的错误,但这是在功能中添加小的自定义更改而不是复制/粘贴整个方法来进行更改的更好方法.此方法还允许更改基本 jQuery 库,而不会影响您的自定义.

I did something similar to this to override $.each to provide some custom functionality. I'm not sure if this will fix the bug you're having, but it is a better way to add a minor custom change in functionality instead of having to copy/paste the entire method to make one change. This method also allows the base jQuery library to change without affecting your customization.

这篇关于如何编辑 jquery 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆