jQuery:如何使用"$"而不是"jQuery"? [英] jQuery: How can I use "$" instead of "jQuery"?

查看:92
本文介绍了jQuery:如何使用"$"而不是"jQuery"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有简单的jQuery,但仍然出现此错误:

I have simple jQuery in my site, yet I keep getting this error:

Uncaught TypeError: Property '$' of object [object DOMWindow] is not a function

仅当我使用"$"而不是"jQuery"时才会出现错误.

The error only appears if I use "$" instead of "jQuery".

// This works
jQuery(document).ready(function() {
    jQuery('#pass').keyup( ... );
});

// This doesn't
$(document).ready(function() {
    $('#pass').keyup( ... );
});

我需要做任何事情来使用"$"吗?

Do I need to do anything to use "$"?

推荐答案

您可以包装代码:

(function($) {
    // here $ would be point to jQuery object
    $(document).ready(function() {
        $('#pass').keyup( ... );
    });
})(jQuery);

这篇关于jQuery:如何使用"$"而不是"jQuery"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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