Replace "$" (dollar sign) with "jQuery"

The first snippet did not work. However, it starts working when replacing all $ (dollar sign) with jQuery (see second fragment). But I really don't understand why? Can anyone explain this to me? Thank you very much!

1st snippet

jQuery.noConflict(); $(document).ready(function(){ $("#insideTable > tbody > tr:odd").addClass("odd"); $("#insideTable > tbody > tr:not(.odd)").hide(); $("#insideTable > tbody > tr:odd").show(); $("#insideTable > tbody > tr.odd").click(function(){ $(this).next().toggle(); $(this).find(".arrow").toggleClass("up"); }); }); 

2nd snippet

  jQuery.noConflict(); jQuery(document).ready(function(){ jQuery("#insideTable > tbody > tr:odd").addClass("odd"); jQuery("#insideTable > tbody > tr:not(.odd)").hide(); jQuery("#insideTable > tbody > tr:odd").show(); jQuery("#insideTable > tbody > tr.odd").click(function(){ jQuery(this).next().toggle(); jQuery(this).find(".arrow").toggleClass("up"); }); }); 
+49
jquery
Jul 19 '11 at 11:23
source share
2 answers

This is because jQuery.noConflict() "exempts" the "$" value from the jQuery associated value. Normally in code, you can use $ as a replacement for "jQuery". If you use noConflict() , you can no longer do this, so you need to replace each "$" with "jQuery" ;,

Many JavaScript libraries use $ as the name of a function or variable, just as jQuery does. In the case of jQuery, $ is just an alias for jQuery, so all functions are available without using $. If we need to use another JavaScript library with jQuery, we can return the $ control back to another library with a call to $ .noConflict ():

you can also create a completely new alias to use

 var myJqueryAlias = jQuery.noConflict(); myJqueryAlias(document).ready(function(){ myJqueryAlias("#insideTable > tbody > tr:odd").addClass("odd"); myJqueryAlias("#insideTable > tbody > tr:not(.odd)").hide(); myJqueryAlias("#insideTable > tbody > tr:odd").show(); myJqueryAlias("#insideTable > tbody > tr.odd").click(function(){ myJqueryAlias(this).next().toggle(); myJqueryAlias(this).find(".arrow").toggleClass("up"); }); }); 
+91
Jul 19 '11 at 11:26
source share

Calling noConflict () removes the relationship between the $ function and jQuery. This means that you can use another JavaScript library, which is also reduced to $ without conflict.

+10
Jul 19 '11 at 11:26
source share



All Articles