jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery未捕获TypeError:无法读取未定义的属性“fn”(匿名函数)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_675_1@所有,
我从我下载的一些代码中收到错误。这是代码
/*----------------------------------------------------------------------*/
/* wl_Alert v 1.1 
/* description: Handles alert Boxes
/* dependency: jquery UI Slider,fadeOutSlide plugin
/*----------------------------------------------------------------------*/


$.fn.wl_Alert = function (method) {
var args = arguments;
return this.each(function () {

    var $this = $(this);


    if ($.fn.wl_Alert.methods[method]) {
        return $.fn.wl_Alert.methods[method].apply(this,Array.prototype.slice.call(args,1));
    } else if (typeof method === 'object' || !method) {
        if ($this.data('wl_Alert')) {
            var opts = $.extend({},$this.data('wl_Alert'),method);
        } else {

            var opts = $.extend({},$.fn.wl_Alert.defaults,method,$this.data());
        }
    } else {
        $.error('Method "' + method + '" does not exist');
    }


    if (!$this.data('wl_Alert')) {

        $this.data('wl_Alert',{});

        //bind click events to hide alert Box
        $this.bind('click.wl_Alert',function (event) {
            event.preventDefault();

            //Don't hide if it is sticky
            if (!$this.data('wl_Alert').sticky) {
                $.fn.wl_Alert.methods.close.call($this[0]);
            }

            //prevent hiding the Box if an inline link is clicked
        }).find('a').bind('click.wl_Alert',function (event) {
            event.stopPropagation();
        });
    } else {

    }
    //show it if it is hidden
    if ($this.is(':hidden')) {
        $this.slideDown(opts.speed / 2);
    }

    if (opts) $.extend($this.data('wl_Alert'),opts);
});

};

$.fn.wl_Alert.defaults = {
speed: 500,sticky: false,onBeforeClose: function (element) {},onClose: function (element) {}
};
$.fn.wl_Alert.version = '1.1';


$.fn.wl_Alert.methods = {
close: function () {
    var $this = $(this),opts = $this.data('wl_Alert');
    //call callBACk and stop if it returns false
    if (opts.onBeforeClose.call(this,$this) === falsE) {
        return false;
    };
    //fadeout and call an callBACk
    $this.fadeOutSlide(opts.speed,function () {
        opts.onClose.call($this[0],$this);
    });
},set: function () {
    var $this = $(this),options = {};
    if (typeof arguments[0] === 'object') {
        options = arguments[0];
    } else if (arguments[0] && arguments[1] !== undefined) {
        options[arguments[0]] = arguments[1];
    }
    $.each(options,function (key,value) {
        if ($.fn.wl_Alert.defaults[key] !== undefined || $.fn.wl_Alert.defaults[key] == null) {
            $this.data('wl_Alert')[key] = value;
        } else {
            $.error('Key "' + key + '" is not defined');
        }
    });

}
};

//to create an alert Box on the fly
$.wl_Alert = function (text,cssclass,insert,after,options) {
//go thru all
$('div.alert').each(function () {
    var _this = $(this);
    //...and hide if one with the same text is allready set
    if (_this.text() == text) {
        _this.slideUp($.fn.wl_Alert.defaults.speed);
    }
});

//create a new DOM element and inject it
var al = $('<div class="alert ' + cssclass + '">' + text + '</div>').hide();
(after) ? al.appendTo(insert).wl_Alert(options) : al.prependTo(insert).wl_Alert(options);

//return the element
return al;
};

有人曾经看过这种类型的错误吗?我该如何解决这样的事情?感谢您有任何建议!

解决方法

尝试这个:
(function ( $ ) { 

    // put all that "wl_alert" code here   

}( jQuery ));

因此,$变量显然已损坏,但jQuery变量仍应引用jQuery对象。 (在正常情况下,$和jQuery变量都指向(相同)jQuery对象。)

您不必在整个代码中使用jQuery名称替换$ name,您可以使用IIFE手动进行名称别名。因此,外部变量jQuery与函数内的$变量进行别名。

这是一个简单的例子来帮助你理解这个概念:

var foo = 'Peter';

(function ( bar ) {

    bar // evaluates to 'Peter'

}( foo ));

大佬总结

以上是大佬教程为你收集整理的jQuery未捕获TypeError:无法读取未定义的属性“fn”(匿名函数)全部内容,希望文章能够帮你解决jQuery未捕获TypeError:无法读取未定义的属性“fn”(匿名函数)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。