Jquery中的(function($){...})(jQuery)

当你第一眼看到“(function($){...})(jQuery)”的时候,你有什么感觉?呵呵呵,我当时还是止不住的从心底里骂了一句——操,这他妈什么劳什子。时过境迁,对于现在无比倚重Jquery的我,自感当时的自己是那么的无知,今天忙里偷闲,解释一下究竟“(function($){...})(jQuery)”该怎样理解:

        代码一:

  1. <html> 
  2.     <head> 
  3.         <title>代码一</title> 
  4.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  5.         <script type="text/javascript"> 
  6.             (function(name){ 
  7.                 alert(name); 
  8.             })("GaoHuanjie"); 
  9.         </script> 
  10.     </head> 
  11.     <body> 
  12.  
  13.     </body> 
  14. </html> 
<html>
	<head>
		<title>代码一</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<script type="text/javascript">
			(function(name){
				alert(name);
			})("GaoHuanjie");
		</script>
	</head>
	<body>

	</body>
</html>

        说明:在浏览器中运行上面代码,会弹出一个对话框,对话框的内容为GaoHuanjie。

        上面代码中执行的JavaScript脚本和“(function($){...})(jQuery)”是一样的,也就是说“(function($){...})(jQuery)”中的函数会自动被执行,那么究竟该怎样理解“(function($){...})(jQuery)”呢?“(function($){...})(jQuery)”实际上表示的是已处于调用状态的匿名函数:function($){...}是定义的匿名函数,参数为$(之所以将参数声明为$是为了不与其他库冲突);为了调用该函数则在该匿名函数的后面添上了括号和实参(这里为jQuery),但又由于操作符的优先级,函数本身也需要用括号,所以又为匿名函数添加了括号。
        释疑解惑:

        一、我运行下面代码为什么报错——jQuery is not defined:

  1. <html> 
  2.     <head> 
  3.         <title>代码二</title> 
  4.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  5.         <script type="text/javascript"> 
  6.             (function($){ 
  7.                 alert($); 
  8.             })(jQuery); 
  9.         </script> 
  10.     </head> 
  11.     <body> 
  12.  
  13.     </body> 
  14. </html> 
<html>
	<head>
		<title>代码二</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<script type="text/javascript">
			(function($){
				alert($);
			})(jQuery);
		</script>
	</head>
	<body>

	</body>
</html>
        嗯嗯,浏览器在解析上述js脚本时确实会出现问题,假如为jQuery添加英文双引号,再次使用浏览器运行上述脚本则不会出错,为什么不加就会出错呢,呵呵呵,其实这里的jQuery还是一个变量,如果为上述代码引入jQuery库,再次使用浏览器运行上述代码则不会出现问题:

  1. <html> 
  2.     <head> 
  3.         <title>代码三</title> 
  4.         <script type="text/javascript" src="./jquery-1.6.2.js"></script> 
  5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  6.         <script type="text/javascript"> 
  7.             (function($){ 
  8.                 alert($); 
  9.             })(jQuery); 
  10.         </script> 
  11.     </head> 
  12.     <body> 
  13.  
  14.     </body> 
  15. </html> 
<html>
	<head>
		<title>代码三</title>
		<script type="text/javascript" src="./jquery-1.6.2.js"></script>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<script type="text/javascript">
			(function($){
				alert($);
			})(jQuery);
		</script>
	</head>
	<body>

	</body>
</html>

        【0分下载资源

        在引入jQuery库后之所以不会出错是因为jQuery库中定义了jQuery变量。

        二、和$(function(){ })相比,(function($){...})(jQuery)的执行时机也是在网页DOM加载完毕后才执行吗?

        不是的,比如如下例子:

  1. <html> 
  2.     <head> 
  3.         <title>代码四</title> 
  4.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  5.         <script type="text/javascript"> 
  6.             (function(){ 
  7.                 alert(document.getElementById("name").value); 
  8.             })(); 
  9.         </script> 
  10.     </head> 
  11.     <body> 
  12.         <input type="hidden" id="name" name="name" value="GaoHuanjie" /> 
  13.     </body> 
  14. </html> 
<html>
	<head>
		<title>代码四</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<script type="text/javascript">
			(function(){
				alert(document.getElementById("name").value);
			})();
		</script>
	</head>
	<body>
		<input type="hidden" id="name" name="name" value="GaoHuanjie" />
	</body>
</html>
        上述例子在浏览器中运行的时候会报错,究其原因(function(){...})()函数的执行时机并不是在DOM加载完毕后才执行,而是随着页面自上而下来执行,如果将其改成如下代码则再次使用浏览器运行脚本则不会出现问题:
  1. <html> 
  2.     <head> 
  3.         <title>代码五</title> 
  4.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  5.     </head> 
  6.     <body> 
  7.         <input type="hidden" id="name" name="name" value="GaoHuanjie" /> 
  8.         <script type="text/javascript"> 
  9.             (function(){ 
  10.                 alert(document.getElementById("name").value); 
  11.             })(); 
  12.         </script> 
  13.     </body> 
  14. </html> 
<html>
	<head>
		<title>代码五</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	</head>
	<body>
		<input type="hidden" id="name" name="name" value="GaoHuanjie" />
		<script type="text/javascript">
			(function(){
				alert(document.getElementById("name").value);
			})();
		</script>
	</body>
</html>

        一句话:“$(function(){ });”用于存放操作DOM对象的代码,执行其中代码时DOM对象已存在;“(function(){})(jQuery);”用于存放开发插件的代码,执行其中代码时DOM不一定存在。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值