部分译文 - Summary of Ways to Reference the jQuery Function
2016-11-01 14:38
567 查看
文章选取自JQuery官方教程。
章节为“☆ Using jQuery Core ”下的“Avoiding Conflicts with Other Library”。如图1所示。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202006/02/0cb361c62f34f573daf21c50842fb91b)
图1
引用jQuery函数的方法概述
当出现其他库与JQuery函数在使用 $ 变量发生冲突时,以下简要介绍一些(避免冲突并正常)引用jQuery函数的方法:
创建一个新别名
由于jQuery.noConflict()方法返回一个jQuery函数的引用,故可用你喜欢的任意变量“捕获”它:
通过把你的代码打包进一个立即执行函数表达式,你可以继续使用标准 $ 符;在编辑者无法知道 $ 被其他库占用的情况下,这也是一种标准的jQuery插件编辑格式。查看插件部分获取更多有关编写插件的信息。
注意,当使用上述方法时,你将不能在这个立即执行函数表达式内部使用prototype.js方法。 $ 将被视作jQuery的引用,而不是prototype.js。
使用传递给jQuery( document ).ready()函数的实参
章节为“☆ Using jQuery Core ”下的“Avoiding Conflicts with Other Library”。如图1所示。
图1
引用jQuery函数的方法概述
当出现其他库与JQuery函数在使用 $ 变量发生冲突时,以下简要介绍一些(避免冲突并正常)引用jQuery函数的方法:
创建一个新别名
由于jQuery.noConflict()方法返回一个jQuery函数的引用,故可用你喜欢的任意变量“捕获”它:
<script src="prototype.js"></script> <script src="jquery.js"></script> <script> // 将 $ 让还给prototype.js; 为jQuery创建新别名. var $jq = jQuery.noConflict(); </script>使用一种立即执行的函数表达式
通过把你的代码打包进一个立即执行函数表达式,你可以继续使用标准 $ 符;在编辑者无法知道 $ 被其他库占用的情况下,这也是一种标准的jQuery插件编辑格式。查看插件部分获取更多有关编写插件的信息。
<pre name="code" class="javascript"><!-- 在一个立即执行函数表达式内部使用$ --><script src="prototype.js"></script><script src="jquery.js"></script><script> jQuery.noConflict(); (function( $ ) { // 你的jQuery代码写在这里,使用 $ 符 })( jQuery ); </script>
注意,当使用上述方法时,你将不能在这个立即执行函数表达式内部使用prototype.js方法。 $ 将被视作jQuery的引用,而不是prototype.js。
使用传递给jQuery( document ).ready()函数的实参
<script src="jquery.js"></script> <script src="prototype.js"></script> <script> jQuery(document).ready(function( $ ) { // 你的jQuery代码写在这里, 使用 $ 引用jQuery. }); </script>或者使用DOM ready函数的简洁语法:
<script src="jquery.js"></script> <script src="prototype.js"></script> <script> jQuery(function($){ // 你的jQuery代码写在这里, 使用 $ }); </script>
相关文章推荐
- A summary of the ways to call external programs
- Question 16: Which of the following methods can a developer use to override the default terminate() function in C++?
- Ten ways to speed up the download time of your web pages
- Git 管理多个分支的公共部分 How to manage the common part of multiple branches
- About:How to write the declaration of pointers and references rightly.
- 20 ways to test the login function
- Ten ways to speed up the download time of your web pages
- A function to retrieve the directory of a program
- (NOT CLEAR)Question 44: What member function of std::fstream could a C++ developer invoke in order to change the target output f
- Given the string of parentheses only, write the function to check if they are balanced. ((())) is ba
- [Javascript] A function works like 'print_r()' in PHP to print out the details of an object for JS debugging
- 【转】[jQuery]30+ Brand New jQuery Plugins To Change the Look and Feel of Your Website
- 转载:The Essential List of JavaScript, jQuery, HTML & CSS Books to Make You a Better Web Developer
- xxxx.accessor: The reference to 'xxxx' was not found in the list of this projects references
- if the reference of the thread is set to null.
- [jQuery]30+ Brand New jQuery Plugins To Change the Look and Feel of Your Website
- [PHP] The reference of function
- [HTML/CSS] Ten ways to speed up the download time of your web pages - Trenton Moss
- Git 管理多个分支的公共部分 How to manage the common part of multiple branches
- Write a function to find the depth of a binary tree