IT技术博客大学习 共学习 共进步

jQuery之保证你的代码安全

WEB前端开发 2010-10-14 01:06:40 浏览 3,441 次

    在多人合作开发中一定要确保变量,对象,函数等命名不要冲突:

    方法一:当别人使用了其他的js库,并该库使用了”$”变量,那么我们可以使用noConflict()方法:

var j = jQuery.noConflict();
// Now, instead of $, we use j.
j('#someDiv').hide();
// The line below will reference some other library's $ function.
$('someDiv').style.display = 'none';

    方法二,把你的代码放在一个匿名函数里面,然后把jQuery作为参数传递给它,那么在这个函数体中的$是不会影响外面或者被外面影响的。

(function($) {
// Within this function, $ will always refer to jQuery
})(jQuery);

    方法三,通过ready方法传递$

jQuery(document).ready(function($) {
// $ refers to jQuery
});
// $ is either undefined, or refers to some other library's function.

    或者使用简写:

$(function() {
// let's get up in heeya
});

建议继续学习

  1. JQuery实现Excel表格呈现 (阅读 48,161)
  2. 分享一个JQUERY颜色选择插件 (阅读 14,062)
  3. jQuery插件---轻量级的弹出窗口wBox. (阅读 10,621)
  4. 10个强大的Ajax jQuery文件上传程序 (阅读 8,720)
  5. jQuery性能优化指南 (阅读 8,645)
  6. jQuery的data()方法 (阅读 8,501)
  7. jQuery Color Animations颜色动画插件 (阅读 8,341)
  8. 精于图片处理的10款jQuery插件 (阅读 7,260)
  9. 配合jquery实现异步加载页面元素 (阅读 6,281)
  10. jQuery中getJSON跨域原理详解 (阅读 6,261)