The origin of t...LOGIN

The origin of the $ symbol in jQuery

$ is a famous jQuery symbol. In fact, jQuery encapsulates all functions in a global variable jQuery, and $ is also a legal variable name. It is an alias of the variable jQuery:

  • window.jQuery ; // jQuery(selector, context)

  • ##window.$; // jQuery(selector, context)

  • ##$ === jQuery ; // true
  • ##typeof($); // 'function'

  • $ is essentially a function, but the function It is also an object, so in addition to being directly called, $ can also have many other attributes.

Note: The $function name you see may not be jQuery(selector, context), because many JavaScript compression tools can rename function names and parameters, so the compressed jQuery source code $function may become a(b,c).

Most of the time, we use $ directly (because it is easier to write). However, if the $ variable is unfortunately occupied and cannot be changed, then we can only ask jQuery to hand over the $ variable, and then we can only use the jQuery variable:

$; // jQuery(selector, context)
  • ##jQuery.noConflict();

  • ##$; // undefined

  • jQuery; // jQuery(selector, context)

  • The principle of this black magic is that jQuery saves the original internally before occupying $ $, when calling jQuery.noConflict(), the originally saved variables will be restored

    Next Section

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#hide").click(function(){ $("p").hide(); }); $("#show").click(function(){ $("p").show(); }); }); </script> </head> <body> <p>欢迎大家来到php.cn</p> <button id="hide">隐藏</button> <button id="show">显示</button> </body> </html>
submitReset Code
ChapterCourseware