(4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)
In some cases, it may prove useful to use more than one JavaScript library on the same page. As many libraries make use of the $identifier (as it is short and convenient), we need a way to prevent collisions between these uses.
在某些场景下,在一个网页上不仅仅使用一个js库可能是很有用的。由于很多库使用$标示符(因为他很短而且很方便),我们需要阻止这些库的冲突。
Fortunately, jQuery provides a method called jQuery.noConflict()to return control of the $identifier back to other libraries. Typical usage of jQuery.noConflict()is as follows:
<script src="prototype.js"></script><script src="jquery.js"></script><script>jQuery.noConflict();</script><script src="myscript.js"></script>First, the other library (Prototype in this example) is included. Then, jQuery itself is included, taking over $for its own use. Next, a call to .noConflict()frees up $, so that control of it reverts to the first included library (Prototype). Now in our custom script, we can use both libraries—but whenever we want to use a jQuery method, we need to write jQueryinstead of $as an identifier.
jQuery(document).ready(function($) {// In here, we can use $ like normal!});或者,使用我们在之前的代码中学到的更加简短的语法:
jQuery(function($) {// Code that uses $.});