$ is not a function

在使用jquery 开发的时候,莫名其妙的碰到一个问题..

$ is not a function .

 

在baidu了好久没有找出答案的时候。google了一下。终于看到了老外的一篇文章。

 

原文如下:

原文地址:http://www.adrogen.com/blog/jquery-conflict-is-not-a-function/

 

Recently I ran into an issue  while trying to use jQuery on a custom WordPress based Content Management System.  This particular system had a number of instances in which the framework was being referenced.  Normally you insert jQuery code using the dollar sign ($) like so:

  1. $(document).ready(function() {   
  2.    $("a").click(function() {   
  3.      alert("Hello world!");   
  4.    });   
  5.  });  

The problem arises when a different system grabs the $ variable.  All of the sudden you have multiple $ variables being used as objects from multiple libraries, resulting in the error console message “$ is not a function”.

Fortunately there is a pretty easy way of fixing this in the form of jQuery.noConflict.  Running this function gives control of the $ variable back to whichever library first implemented it.  This will help to ensure that jQuery won’t conflict with other instances of the $ object in other libraries.

But please note, in doing this, you are re-assigning the variable so you will only be able to access jQuery commands using the ‘jQuery’ variable (which has just replaced ‘$’).  So our above code example would look something like this:

  1. jQuery.noConflict();   
  2. jQuery(document).ready(function() {   
  3.    jQuery("a").click(function() {   
  4.      alert("Hello world!");   
  5.    });   
  6.  });  

It doesn’t seem to be a very common problem and only occurs when multiple instances of the object crop up, but that’s easy to do in WordPress when multiple plugins begin conflicting with each other.   Follow the link for the  full jQuery documentation on jQuery.noConflict

 

 

 

把 $ 全部替换为 JQuery 就OK 了。

 

其中关键的在于 jQuery.noConflict();   这句话

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值