首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >jQuery未捕获TypeError:无法读取未定义(匿名函数)的属性“”fn“”

jQuery未捕获TypeError:无法读取未定义(匿名函数)的属性“”fn“”
EN

Stack Overflow用户
提问于 2012-05-30 06:10:47
回答 11查看 269.7K关注 0票数 66

总之,我从我下载的一些代码中得到一个错误。代码如下:

代码语言:javascript
复制
/*----------------------------------------------------------------------*/
/* 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;
};

以前有没有人见过这种类型的错误?我该如何解决这样的问题呢?谢谢你的任何建议!

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2012-05-30 07:01:33

试试这个:

代码语言:javascript
复制
(function ( $ ) { 

    // put all that "wl_alert" code here   

}( jQuery ));

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

不必在整个代码中将$名称替换为jQuery名称,只需使用IIFE手动为该名称设置别名。因此,外部变量jQuery使用函数内部的$变量作为别名。

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

代码语言:javascript
复制
var foo = 'Peter';

(function ( bar ) {

    bar // evaluates to 'Peter'

}( foo ));
票数 62
EN

Stack Overflow用户

发布于 2018-02-20 23:34:02

尝试在bootstrap.js之前调用jQuery库

代码语言:javascript
复制
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
票数 88
EN

Stack Overflow用户

发布于 2014-04-17 01:16:27

您可以使用noConflict函数:

代码语言:javascript
复制
var JJ= jQuery.noConflict(); 
JJ('.back').click(function (){
    window.history.back();
}); 

将所有$更改为JJ。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10807430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档