{"id":3520,"date":"2013-07-17T22:57:27","date_gmt":"2013-07-17T14:57:27","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=3520"},"modified":"2018-01-02T03:15:19","modified_gmt":"2018-01-01T19:15:19","slug":"jquery-%e5%8e%9f%e7%90%86-%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2013\/07\/jquery-%e5%8e%9f%e7%90%86-%e6%9c%ba%e5%88%b6\/","title":{"rendered":"jQuery\u8bde\u751f\u8bb0-\u539f\u7406\u4e0e\u673a\u5236"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/\">http:\/\/www.zhangxinxu.com<\/a><br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=3520\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=3520<\/a><\/p>\n<h3>\u4e00\u3001\u770b\u4f3c\u5076\u7136\u7684\u4e1c\u897f\u5b9e\u9645\u662f\u5fc5\u7136\u4f1a\u53d1\u751f\u7684<\/h3>\n<p>\u6211\u5927\u5b66\u65f6\u5019\u5728\u56fe\u4e66\u9986\u7ffb\u8fc7\u4e00\u672c\u5f88\u7834\u65e7\u7684\u4e66\uff0c\u8bb2\u751f\u7269\u7406\u8bba\u7684\uff0c\u4e3b\u8981\u5185\u5bb9\u5c31\u662f\u63a2\u8ba8\u751f\u547d\u7684\u4ea7\u751f\u662f\u5076\u7136\u8fd8\u662f\u5fc5\u7136\u3002\u91cc\u9762\u5f88\u591a\u4e9a\u91cc\u58eb\u591a\u5fb7\u90fd\u770b\u4e0d\u61c2\u7684\u516c\u5f0f\u8ba1\u7b97\u4ec0\u4e48\u7684\uff0c\u8fd8\u6709\u6a21\u62df\u539f\u59cb\u5730\u7403\u73af\u5883\u51fa\u73b0\u4e86\u6709\u673a\u7269\u7684\u5b9e\u9a8c\u4ec0\u4e48\u7684 <img decoding=\"async\" src=\"http:\/\/mat1.gtimg.com\/www\/mb\/images\/face\/183.gif\" align=\"absmiddle\">\u3002\u603b\u4e4b\uff0c\u4e66\u8bba\u8ff0\u7684\u89c2\u70b9\u662f\uff1a\u201c\u5728\u5f53\u65f6\u7684\u5730\u7403\u73af\u5883\u4e0b\uff0c\u751f\u547d\u7684\u4ea7\u751f\u662f\u5fc5\u7136\u7684\uff01\u201d \u65e0\u6570\u6b21\u673a\u4f1a\u7684\u5076\u7136\u6761\u4ef6\u3001\u65e0\u6570\u6b21\u5316\u5408\u7269\u7684\u76f8\u9047\u53cd\u5e94\u7b49\u5fc5\u5b9a\u4f1a\u4ea7\u751f\u6709\u673a\u7269\uff0c\u518d\u6709N\u591a\u5076\u7136\uff0c\u6709\u673a\u7269\u5fc5\u7136\u5f62\u6210\u4e86\u6709\u673a\u4f53\u2026\u2026<\/p>\n<p>\u8fd9\u79cd\u7406\u8bba\u7c7b\u4f3c\u4e8e\uff0c\u4f60\u662f\u4e2a\u8fc7\u9a6c\u8def\u975e\u5e38\u5c0f\u5fc3\u7684\u4eba\uff0c\u4e14\u4f60\u4e07\u5bff\u65e0\u7586\uff0c\u9664\u4e86\u6015\u88ab\u6c7d\u8f66\u649e\u3002\u7ed9\u4f60100\u4e07\u5e74\u7684\u5bff\u547d\uff0c\u4f60\u6700\u540e\u5fc5\u7136\u8fd8\u662f\u88ab\u8f66\u649e\u6b7b\u3002<\/p>\n<p>\u5982\u679c\u4ee5\u8fd9\u79cd\u7406\u8bba\u6765\u770bjQuery\u7684\u51fa\u73b0\uff0c\u7ed3\u8bba\u4e5f\u5e94\u8be5\u662f<strong>\u5fc5\u7136\u7684<\/strong>\uff01<\/p>\n<h3>\u4e8c\u3001\u9700\u6c42\u3001\u52a8\u529b\u3001\u53d1\u5c55\u3001\u4e8b\u7269\u4ea7\u751f\u4e0ejQuery\u7684\u8bde\u751f<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201307\/daoxiaomian.jpg\" width=\"336\" height=\"248\" alt=\"\u5200\u524a\u9762\u673a\u5668\u4eba\" class=\"alignnone\" \/><\/p>\n<p>\u4e00\u4e2a\u6210\u719f\u7684\u4e1c\u897f\u663e\u7136\u4e0d\u662f\u4e00\u53e3\u6c14\u5c31\u51fa\u6765\u7684\uff0c\u6240\u8c13\u201c\u4e00\u94f2\u5b50\u6316\u4e0d\u4e86\u4e00\u53e3\u4e95\u201d\uff0c\u6211\u60f3jQuery\u7684\u539f\u4f5c\u8005\u518d\u5929\u624d\uff0c\u4e5f\u662f\u5faa\u5e8f\u6e10\u8fdb\u8fc7\u6765\u7684\uff0c\u5982\u4f55\u4e2a\u5faa\u5e8f\u6e10\u8fdb\u6cd5\uff0c\u6211\u60f3\uff0c\u5f88\u6709\u53ef\u80fd\u5c31\u662f\u9700\u6c42\u9a71\u52a8\u800c\u4ea7\u751f\u7684\uff0c\u597d\u6bd4\u4e0a\u56fe\u5200\u524a\u9762\u673a\u5668\u4eba\uff0c\u636e\u8bf4\u73b0\u5728\u5df2\u7ecf\u7b2c\u516b\u4ee3\u4e86\uff01<\/p>\n<p><strong>1. gelElementById\u592a\u957f\u4e86<\/strong><br \/>\n\u9875\u9762\u4e0a\u6709\u4e2a\u6309\u94ae\uff0c\u8fd8\u6709\u4e2a\u56fe\u7247\uff0c\u6211\u60f3\u70b9\u51fb\u6309\u94ae\u56fe\u7247\u9690\u85cf\uff0c\u5982\u4e0bHTML\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;button id=\"button\"&gt;\u70b9\u51fb\u6211&lt;\/button&gt;\n&lt;img id=\"image\" src=\"xxx.jpg\">\n<\/pre>\n<\/div>\n<p>\u4e8e\u662f\uff0c\u6211\u7684\u811a\u672c\u53ef\u80fd\u5c31\u8fd9\u6837\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>\nvar button = document.getElementById(\"button\")\n    , image = document.getElementById(\"image\")\n\nbutton.onclick = function() {\n    image.style.display = \"none\";\n};\n<\/pre>\n<\/div>\n<p>\u6709\u4f55\u95ee\u9898\uff1f\u4eba\u51e0\u4e4e\u90fd\u662f\u5929\u751f\u7684\u201c\u61d2\u60f0\u8005\u201d\uff0c<code>document.getElementById<\/code>\u540d\u79f0\u957f\u4e14\u91cd\u590d\u51fa\u73b0\uff0c\u597d\u50cf\u5230\u4e86\u516c\u53f8\u53d1\u73b0\u5361\u6ca1\u5e26\u53c8\u56de\u5bb6\u91cd\u65b0\u62ff\u5361\u7684\u611f\u89c9\uff0c\u6211\u5e0c\u671b\u8d8a\u7b80\u5355\u8d8a\u597d\u3002\u6069\uff0c<img decoding=\"async\" src=\"http:\/\/img.t.sinajs.cn\/t4\/appstyle\/expression\/ext\/normal\/e9\/sk_thumb.gif\" align=\"absmiddle\"> \u6211\u5f88\u559c\u6b22\u94b1\uff0c<code>$<\/code>\u8fd9\u4e2a\u7b26\u53f7\u6211\u5f88\u559c\u6b22\uff0c\u6211\u6253\u7b97\u6539\u9020\u4e00\u756a\uff0c\u7b80\u5316\u6211\u7684\u5de5\u4f5c\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var $ = function(id) {\n    return document.getElementById(id);\n};\n\n<span style=\"color:#cd0000;\">$(\"button\")<\/span>.onclick = function() {\n    <span style=\"color:#cd0000;\">$(\"image\")<\/span>.style.display = \"none\";\n};\n<\/pre>\n<\/div>\n<p>\u8fd9\u91cc\u7684<code>$()<\/code>\u5c31\u662f\u6700\u7b80\u5355\u7684\u5305\u88c5\u5668\uff0c\u53ea\u662f\u8fd4\u56de\u7684\u662f\u539f\u751f\u7684DOM\u5bf9\u8c61\u3002<\/p>\n<p><strong>2. \u6211\u9700\u8981\u4e00\u4e2a\u7b80\u6d01\u7684\u6697\u53f7\uff0c\u5c31\u50cf\u201c\u829d\u9ebb\u5f00\u95e8\u201d<\/strong><br \/>\n\u540e\u6765\u9875\u9762\u590d\u6742\u4e86\uff0c\u70b9\u51fb\u4e00\u4e2a\u6309\u94ae\uff0c\u67092\u5f20\u56fe\u7247\u8981\u9690\u85cf\u3002<\/p>\n<div class=\"zxx_code\">\n<pre>$(\"button\").onclick = function() {\n    $(\"image1\").style.display = \"none\";\n    $(\"image2\").style.display = \"none\";\n};<\/pre>\n<\/div>\n<p>\u597d\u50cf\u53c8\u770b\u89c1\u957f\u957f\u7684\u91cd\u590d\u7684\u4e1c\u897f\uff0c<code>xxx.style.display = \"none\"<\/code>, \u4e3a\u4ec0\u4e48\u6bcf\u6b21\u5f00\u95e8\u90fd\u8981\u4ece\u5305\u91cc\u627e\u5230\u94a5\u5319\u3001\u5bf9\u51c6\u63d2\u53e3\u63d2\u8fdb\u53bb\u3001\u8fd8\u8981\u5de6\u626d\u626d\u53f3\u626d\u626d\u5462\uff1f\u4e00\u6b21\u8fd8\u597d\uff0c\u5929\u5929\u7ecf\u5e38\u8001\u662f\u8fd9\u6837\u600e\u53d7\u5f97\u4e86\u3002\u8bbe\u60f3\uff0c\u8981\u662f\u6709\u4e2a\u829d\u9ebb\u5f00\u95e8\u7684\u6697\u53f7\u5c31\u597d\u4e86\uff0c\u201copen\u5f00\u201d\uff0c\u58f0\u97f3\u8bc6\u522b\uff0c\u95e8\u81ea\u52a8\u5f00\u4e86\uff0c\u591a\u7701\u5fc3\u3002<\/p>\n<p>\u8fd9\u91cc\u6bcf\u6b21\u9690\u85cf\u90fd\u8981<code>xxx.style.display = \"none\"<\/code>, \u6bd4\u6bcf\u5929\u62ff\u94a5\u5319\u5f00\u95e8\u8fd8\u8981\u70e6\uff0c\u6211\u5e0c\u671b\u6709\u4e00\u4e2a\u5feb\u6377\u7684\u65b9\u5f0f\uff0c\u4f8b\u5982\uff0c\u201chide\u9690\u201d\uff0c\u8bed\u53e5\u8bc6\u522b\uff0c\u5143\u7d20\u81ea\u52a8\u9690\u85cf\uff0c\u591a\u7701\u5fc3\u3002<\/p>\n<p>\u5c31\u662f\u8981\u53d8\u6210\u4e0b\u9762\u7684\u6548\u679c\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>$(\"button\").onclick = function() {\n    $(\"image1\")<span style=\"color:#cd0000;\">.hide()<\/span>;\n    $(\"image2\")<span style=\"color:#cd0000;\">.hide()<\/span>;\n};<\/pre>\n<\/div>\n<p><strong>3. \u5982\u4f55\u8bc6\u522b\u201c\u829d\u9ebb\u5f00\u95e8\u201d\u7684\u6697\u53f7<\/strong><br \/>\n<code>$(\"image1\")<\/code>\u672c\u8d28\u662f\u4e2aDOM\u5143\u7d20\uff0c<code>$(\"image1\").hide()<\/code>\u4e5f\u5c31\u662f\u5728DOM\u5143\u7d20\u4e0a\u6269\u5c55\u4e00\u4e2a<code>hide<\/code>\u65b9\u6cd5\uff0c\u8c03\u7528\u5373\u9690\u85cf\u3002<\/p>\n<p>\u54e6\uff0c\u6269\u5c55\uff0c\u7acb\u9a6c\u60f3\u5230\u4e86JS\u4e2d\u7684<code>prototype<\/code>\u539f\u578b\u3002<span class=\"s\">\/\/zxx: \u8001\u677f\uff0c\u73b0\u5728\u6ee1\u5927\u8857\u4ec0\u4e48\u83dc\u6700\u4fbf\u5b9c\u3002\u8001\u677f\uff1a\u539f\u578b\u554a\uff0c\u90fd\u6cdb\u6ee5\u4e86\uff01<\/span><\/p>\n<div class=\"zxx_code\">\n<pre>\n<span style=\"color:#cd0000;\">HTMLElement.prototype<\/span>.hide = function() {\n    this.style.display = \"none\";\n};\n<\/pre>\n<\/div>\n<p>\u4e0a\u9762\u4ee3\u7801\u7684<a href=\"http:\/\/www.zhangxinxu.com\/study\/201307\/image-hide.html\" class=\"a_link\" target=\"_blank\">demo\u5730\u5740<\/a>\u5e94\u8be5\u4e0d\u4f1a\u88ab\u4eba\u770b\u5230\u5427\u2026\u2026<\/p>\n<p>\u867d\u7136\u5728\u8eab\u4f53\u4e0a\u94bb\u4e86\u4e2a\u7a9f\u7abf\u63d2\u8fdb\u5165\u4e86\u4e00\u4e2a\u65b9\u6cd5\uff0c\u6bd5\u7adf\u6d4f\u89c8\u5668\u6709\u6709\u6548\u679c\u554a\uff0c\u5207\u80a4\u4e4b\u75db\u5c31\u4e0d\u7b97\u4ec0\u4e48\u4e86\u3002\u4f46\u662f\uff0c\u6211\u4eec\u662f\u5728\u6cf1\u6cf1\u5929\u671d\uff0c\u5f88\u591aIE6~IE8\u8001\u987d\u56fa\uff0c\u8fd9\u4e9b\u8001\u4e1c\u897f\u4e0d\u8ba4\u8bc6<code>HTMLElement<\/code>\uff0c\u5bf9\u4e8e<code>HTMLElement<\/code>\u81ea\u6b8b\u6269\u5c55\u4e4b\u6cd5\u6839\u672c\u7406\u89e3\u4e0d\u4e86\uff0c\u800c\u8fd9\u4e9b\u8001\u5bb6\u4f19\u638c\u7ba1\u4e86\u534a\u58c1\u6c5f\u5c71\u3002\u5509\uff0c\u9762\u5bf9\u73b0\u5b9e\uff0c\u5143\u7d20\u76f4\u63a5\u6269\u5c55\u662f\u884c\u4e0d\u901a\u4e86\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u7531\u4e8e\u517c\u5bb9\u6027\uff0c\u6211\u4eec\u9700\u8981\u60f3\u5176\u4ed6\u6269\u5c55\u65b9\u6cd5\u3002<\/p>\n<p><strong>4. \u6761\u6761\u5927\u8def\u901a\u7f57\u9a6c\uff0c\u6b64\u5904\u4e0d\u7559\u7237\uff0c\u81ea\u6709\u7559\u7237\u5904<\/strong><br \/>\n\u867dIE6~IE8\u4e0d\u8bc6<code>HTMLElement<\/code>\u539f\u578b\u6269\u5c55\uff0c\u4f46\u662f\uff0c<code>Function<\/code>\u7684\u539f\u578b\u6269\u5c55\u5176\u8ba4\u8bc6\u554a\u3002\u7ba1\u4e0d\u7ba1\u7528\uff0c\u6682\u65f6\u4e0d\u5f97\u800c\u77e5\uff0c\u5148\u968f\u4fbf\u641e\u4e2a\u7b80\u5355\u7684\u8bd5\u8bd5\u5457~<\/p>\n<div class=\"zxx_code\">\n<pre>var F = function() {};\nF.prototype.hide = function() {\n    this?.style.display = \"none\";\n};\n\nnew F().hide();    <span style=\"color:green;\">\/\/ \u8fd9\u4e2a\u5b9e\u73b0\u9690\u85cf\uff1f<\/span><\/pre>\n<\/div>\n<p>\u672c\u6587\u81f3\u5c11\u8fd8\u6709\u4e00\u534a\u7684\u5185\u5bb9\uff0c\u4f46\u662f\uff0c\u5168\u6587\u7684\u6700\u96be\u70b9\u5c31\u5728\u8fd9\u91cc\u7684\uff0c\u5bf9<code>new F()<\/code>\u7684\u8ba4\u8bc6\u548c\u7406\u89e3\u3002<\/p>\n<p style=\"text-align:left;\">\u4e0a\u9762\u7684\u4ee3\u7801\uff0cnew F()\u60a8\u53ef\u4ee5\u770b\u505a\u662f<code>this?.style<\/code>\u8fd9\u91cc\u7684<code>this<\/code>. \u60a8\u53ef\u80fd\u4f1a\u8df3\u8d77\u6765\u62a2\u7b54\u9053\uff1a\u201c\u90a3<code>new F()<\/code>\u7684<code>return<\/code>\u503c = <code>DOM\u5143\u7d20<\/code>\u4e0d\u5c31\u5b8c\u4e8bOK\u5566\uff01\u2014\u2014 <code>this.style.hide<\/code> = <code>new F().style.hide<\/code> = <code>DOM.style.hide<\/code>\u201d\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201307\/henshahentianzhen.jpg\" width=\"399\" height=\"267\" alt=\"\u5f88\u50bb\u5f88\u5929\u771f\" class=\"alignnone\" \/><\/p>\n<blockquote>\n<p>\u53ea\u8981<code>new<\/code>\u8868\u8fbe\u5f0f\u4e4b\u540e\u7684<code>constructor<\/code>\u8fd4\u56de\uff08<code>return<\/code>\uff09\u4e00\u4e2a\u5f15\u7528\u5bf9\u8c61\uff08\u6570\u7ec4\uff0c\u5bf9\u8c61\uff0c\u51fd\u6570\u7b49\uff09\uff0c\u90fd\u5c06\u8986\u76d6new\u521b\u5efa\u7684\u533f\u540d\u5bf9\u8c61\uff0c\u5982\u679c\u8fd4\u56de\uff08<code>return<\/code>\uff09\u4e00\u4e2a\u539f\u59cb\u7c7b\u578b\uff08\u65e0<code>return<\/code>\u65f6\u5176\u5b9e\u4e3a<code>return<\/code>\u539f\u59cb\u7c7b\u578b<code>undefined<\/code>\uff09\uff0c\u90a3\u4e48\u5c31\u8fd4\u56de<code>new<\/code>\u521b\u5efa\u7684\u533f\u540d\u5bf9\u8c61\u3002<\/p>\n<\/blockquote>\n<p>\u4e0a\u9762\u7684\u5f15\u7528\u6765\u81ea<a href=\"http:\/\/www.planabc.net\/2008\/02\/20\/javascript_new_function\/\">\u8fd9\u91cc<\/a>\u3002\u4ec0\u4e48\u610f\u601d\u5462\uff1f\u8bf4\u767d\u4e86\u5c31\u662f\uff0c<code>new F()<\/code>\u5982\u679c\u6ca1\u6709\u8fd4\u56de\u503c(<code>Undefined<\/code>\u7c7b\u578b)\uff0c\u6216\u8fd4\u56de\u503c\u662f5\u79cd\u57fa\u672c\u578b\uff08<code>Undefined<\/code>\u7c7b\u578b\u3001<code>Null<\/code>\u7c7b\u578b\u3001<code>Boolean<\/code>\u7c7b\u578b\u3001<code>Number<\/code>\u7c7b\u578b\u3001<code>String<\/code>\u7c7b\u578b\uff09\u4e4b\u4e00\uff0c\u5219<code>new F()<\/code>\u6211\u4eec\u53ef\u4ee5\u770b\u6210\u662f\u539f\u578b\u6269\u5c55\u65b9\u6cd5\u4e2d\u7684<code>this<\/code>; \u5982\u679c\u8fd4\u56de\u662f\u662f\u6570\u7ec4\u554a\u3001\u5bf9\u8c61\u554a\u4ec0\u4e48\u7684\uff0c\u5219\u8fd4\u56de\u503c\u5c31\u662f\u8fd9\u4e9b\u5bf9\u8c61\u672c\u8eab\uff0c\u6b64\u65f6<code>new F()<\/code> \u2260 <code>this<\/code>\u3002<\/p>\n<p>\u4e3e\u4f8b\u8bf4\u660e\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var F = function(id) {\n    return document.getElementById(id);\n};\n\nnew F(\"image1\") == document.getElementById(\"image1\");    <span style=\"color:green;\">\/\/ true \u8bf4\u660e\u770b\u4e0a\u53bb\u8fd4\u56deDOM\u5bf9\u8c61\uff0c\u5b9e\u9645\u786e\u5b9e\u5c31\u662fDOM\u5bf9\u8c61<\/span><\/pre>\n<\/div>\n<div class=\"zxx_code\">\n<pre>var F = function(id) {\n    return id;\n};\n\nnew F(\"image1\") == \"image1\";    <span style=\"color:green;\">\/\/ false \u8bf4\u660e\u770b\u4e0a\u53bb\u8fd4\u56de\u5b57\u7b26\u4e32\u503c\uff0c\u5b9e\u9645\u5e76\u4e0d\u662f\u5b57\u7b26\u4e32<\/span><\/pre>\n<\/div>\n<p>\u56de\u5230\u4e0a\u9762\u5929\u771f\u7684\u60f3\u6cd5\u3002\u8981\u60f3\u4f7f\u7528<code>prototype.hide<\/code>\u65b9\u6cd5\u4e2d\u7684<code>this<\/code>, \u5076\u4eec\u5c31\u4e0d\u80fd\u8ba9<code>F<\/code>\u51fd\u6570\u6709\u4e71\u4e03\u516b\u7cdf\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<p>\u56e0\u6b64\uff0c<code>new F()<\/code>\u76f4\u63a5\u8fd4\u56deDOM\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u501f\u52a9<code>this<\/code>\u95f4\u63a5\u8c03\u7528\u3002\u6bd4\u65b9\u8bf4\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var F = function(id) {\n    <span style=\"color:#cd0000;\">this.element<\/span> = document.getElementById(id);\n};\nF.prototype.hide = function() {\n    <span style=\"color:#cd0000;\">this.element.style.display<\/span> = \"none\";\n};\n\nnew F(\"image\").hide();    <span style=\"color:green;\">\/\/ \u770b\u4f60\u8fd8\u4e0d\u9690\u85cf<\/span><\/pre>\n<\/div>\n<p>\u4e0a\u9762\u4ee3\u7801\u7684<a href=\"http:\/\/www.zhangxinxu.com\/study\/201307\/image-hide2.html\" class=\"a_link\" target=\"_blank\">demo\u5730\u5740<\/a>\u5e94\u8be5\u4e0d\u4f1a\u88ab\u4eba\u770b\u5230\u5427\u2026\u2026<\/p>\n<p><strong>5. \u66b4\u9732\u4e0e\u91cd\u7528\u5143\u7d20\u83b7\u53d6\u65b9\u6cd5<\/strong><br \/>\n\u4e0a\u9762\u7684\u65b9\u6cd5\uff0c\u5143\u7d20\u7684\u83b7\u53d6\u76f4\u63a5\u5728F\u65b9\u6cd5\u4e2d\uff0c\u4f46\u662f\uff0c\u5b9e\u9645\u60c5\u51b5\uff0c\u8003\u8651\u5230\u517c\u5bb9\u6027\u5b9e\u73b0\uff0c\u5143\u7d20\u83b7\u53d6\u53ef\u80fd\u4f1a\u76f8\u5f53\u590d\u6742\uff0c\u540c\u65f6\u65b9\u6cd5\u79c1\u6709\uff0c\u4e0d\u80fd\u91cd\u5229\u7528\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u628a\u5143\u7d20\u83b7\u53d6\u65b9\u6cd5\u653e\u5728\u539f\u578b\u4e0a\uff0c\u4fbf\u4e8e\u7ba1\u7406\u548c\u91cd\u7528\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var F = function(id) {\n    return this.getElementById(id);\n};\nF.prototype.getElementById = function(id) {\n    <span style=\"color:#cd0000;\">this.element<\/span> = document.getElementById(id);\n    return <span style=\"color:#cd0000;\">this<\/span>;\n};\nF.prototype.hide = function() {\n    this.element.style.display = \"none\";\n};\n\nnew F(\"image\").hide();    <span style=\"color:green;\">\/\/ \u770b\u4f60\u8fd8\u4e0d\u9690\u85cf<\/span><\/pre>\n<\/div>\n<p>\u5143\u7d20\u83b7\u53d6\u65b9\u6cd5\u653e\u5728<code>prototype<\/code>\u4e0a\uff0c\u901a\u8fc7<code>F()<\/code>\u6267\u884c\u3002\u4f60\u53ef\u80fd\u4f1a\u5947\u602a\u4e86\uff0c\u4f60\u521a\u660e\u660e\u8bf4\u201c<code>new F()<\/code>\u76f4\u63a5\u8fd4\u56deDOM\u662f\u4e0d\u53ef\u53d6\u7684\u201d\uff0c\u600e\u4e48\u73b0\u5728\u53c8\u6709<code>return<\/code>\u5462\uff1f\u5927\u5bb6\u52a1\u5fc5\u64e6\u4eae\u773c\u775b\uff0c<code>F.prototype.getElementById<\/code>\u7684\u8fd4\u56de\u503c\u662f<code>this<\/code>\uff0c\u4e5f\u5c31\u662f<code>new F()<\/code>\u7684\u8fd4\u56de\u503c\u662f<code>this<\/code>. \u5f62\u8c61\u70b9\u5c31\u662f<code>new F(\"image\")<\/code>\u51fa\u4e86\u4e00\u62f3\uff0c\u53c8\u53cd\u5f39\u5230\u81ea\u5df1\u8138\u4e0a\u4e86\u3002<\/p>\n<p>\u4e0a\u9762\u4ee3\u7801\u7684<a href=\"http:\/\/www.zhangxinxu.com\/study\/201307\/image-hide3.html\" class=\"a_link\" target=\"_blank\">demo\u5730\u5740<\/a>\u5e94\u8be5\u4e0d\u4f1a\u88ab\u4eba\u770b\u5230\u5427\u2026\u2026<\/p>\n<p><strong>6. \u6211\u4e0d\u559c\u6b22new, \u6211\u559c\u6b22$<\/strong><br \/>\n<code>new F(\"image\")<\/code>\u8fd9\u79cd\u5199\u6cd5\u6211\u597d\u4e0d\u559c\u6b22\uff0c\u6211\u559c\u6b22<code>$<\/code>, \u6211\u5c31\u662f\u559c\u6b22<code>$<\/code>, \u6211\u8981\u6362\u6389\u3002<\/p>\n<p>\u597d\u5427\uff0c\u628a<code>new<\/code>\u4ec0\u4e48\u4ec0\u4e48\u85cf\u5728<code>$<\/code>\u65b9\u6cd5\u4e2d\u628a~<\/p>\n<div class=\"zxx_code\">\n<pre>var $ = function(id) {\n    return new F(id);\n};<\/pre>\n<\/div>\n<p>\u4e8e\u662f\uff0c\u4e0a\u9762\u7684\u56fe\u7247\u9690\u85cf\u7684\u76f4\u63a5\u6267\u884c\u4ee3\u7801\u5c31\u662f\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>$(\"image\").hide();<\/pre>\n<\/div>\n<p>\u4e0a\u9762\u4ee3\u7801\u7684<a href=\"http:\/\/www.zhangxinxu.com\/study\/201307\/image-hide4.html\" class=\"a_link\" target=\"_blank\">demo\u5730\u5740<\/a>\u5e94\u8be5\u4e0d\u4f1a\u88ab\u4eba\u770b\u5230\u5427\u2026\u2026<\/p>\n<p>IE6\u6d4f\u89c8\u5668\u4e5f\u662f\u652f\u6301\u7684\u54e6\uff01\u662f\u4e0d\u662f\u5df2\u7ecf\u6709\u4e9bjQuery\u7684\u6837\u5b50\u5566\uff01<\/p>\n<p><strong>7. \u4f60\u600e\u4e48\u5c31\u4e00\u79cd\u59ff\u52bf\u554a\uff0c\u4eba\u5bb6\u90fd\u817b\u4e86\u8bf6<\/strong><br \/>\n\u5faa\u5e8f\u6e10\u8fdb\u5230\u73b0\u5728\uff0c\u90fd\u662f\u62ff<code>id<\/code>\u6765\u4e3e\u4f8b\u7684\uff0c\u5b9e\u9645\u5e94\u7528\uff0c\u6211\u4eec\u53ef\u80fd\u8981\u4f7f\u7528\u7c7b\u540d\u554a\uff0c\u6807\u7b7e\u540d\u554a\u4ec0\u4e48\u7684\uff0c\u73b0\u5728\uff0c\u4e3a\u4e86\u63a5\u4e0b\u6765\u7684\u7ee7\u7eed\uff0c\u6709\u5fc5\u8981\u652f\u6301\u591a\u4e2a\u201c\u59ff\u52bf\u201d\u3002<\/p>\n<p>\u5728IE8+\u6d4f\u89c8\u5668\u4e2d\uff0c\u6211\u4eec\u6709\u9009\u62e9\u5668API\uff0c<code>document.querySelector<\/code>\u4e0e<code>document.querySelectorAll<\/code>\uff0c\u524d\u8005\u8fd4\u56de\u552f\u4e00<code>Node<\/code>\uff0c\u540e\u8005\u4e3a<code>NodeList<\/code>\u96c6\u5408\u3002\u5927\u7edf\u4e00\u8d77\u89c1\uff0c\u6211\u4eec\u4f7f\u7528\u540e\u8005\u3002\u4e8e\u662f\uff0c\u5c31\u6709\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var F = function(selector, context) {\n    return this.getNodeList(selector, context);\n};\nF.prototype.getNodeList = function(selector, context) {\n    context = context || document;\n    this.element = context.<span style=\"color:#cd0000;\">querySelectorAll<\/span>(selector);\n    return this;\n};\n\nvar $ = function(selector, context) {\n    return new F(selector, context);\n};<\/pre>\n<\/div>\n<p>\u6b64\u65f6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u9009\u62e9\u5668\u4e86\uff0c\u4f8b\u5982\uff0c<code>$(\"body #image\")<\/code>, <code>this.element<\/code>\u5c31\u662f\u9009\u62e9\u7684\u5143\u7d20\u4eec\u3002<\/p>\n<p><strong>8. IE6\/IE7\u80bf\u4e48\u529e\uff1f<\/strong><br \/>\nIE6\/IE7\u4e0d\u8ba4\u8bc6<code>querySelectorAll<\/code>\uff0c\u548b\u529e\uff1f<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/emtion\/ask.png\" width=\"150\" height=\"150\" alt=\"\u600e\u4e48\u529e\uff1f\" class=\"alignnone\" \/><\/p>\n<p>jQuery\u5c31\u4f7f\u7528\u4e86\u4e00\u4e2a\u6bd4\u8f83\u5f3a\u5927\u7684\u9009\u62e9\u5668\u6846\u67b6-<code>Sizzle<\/code>. \u77e5\u9053\u5c31\u597d\uff0c\u91cd\u5728\u6f14\u793a\u539f\u7406\uff0c\u56e0\u6b64\uff0c\u4e0b\u9762\u8fd8\u662f\u4f7f\u7528\u539f\u751f\u7684\u9009\u62e9\u5668API\u793a\u610f\uff0c\u6545demo\u6548\u679c\u9700\u8981IE8+\u6d4f\u89c8\u5668\u4e0b\u67e5\u770b\u3002<\/p>\n<p><strong>8. \u904d\u5386\u662f\u4e2a\u9ebb\u70e6\u4e8b<\/strong><br \/>\n<code>this.element<\/code>\u6b64\u65f6\u7c7b\u578b\u662f<code>NodeList<\/code>, \u56e0\u6b64\uff0c\u76f4\u63a5<code>this.element.style.xxx<\/code>\u7684\u505a\u6cd5\u4e00\u5b9a\u662f\u62a5\u9519\uff0c\u770b\u6765\u6709\u5fc5\u8981\u5faa\u73af\u4e0b\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>F.prototype.hide = function() {\n    var i=0, length = this.element.length;\n    for (; i&lt;length; i+=1) {\n        this.element[i].style.display = \"none\";\n    }    \n};<\/pre>\n<\/div>\n<p>\u4e8e\u662f\u4e4e\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>$(\"img\").hide();  <span style=\"color:green;\">\/\/ \u9875\u9762\u6240\u6709\u56fe\u7247\u90fd\u9690\u85cf\u5566\uff01<\/span><\/pre>\n<\/div>\n<p>\u4e0a\u9762\u4ee3\u7801\u7684<a href=\"http:\/\/www.zhangxinxu.com\/study\/201307\/image-hide5.html\" class=\"a_link\" target=\"_blank\">demo\u5730\u5740<\/a>\u5e94\u8be5\u4e0d\u4f1a\u88ab\u4eba\u770b\u5230\u5427\u2026\u2026<\/p>\n<p>\u5355\u7eaf\u4e00\u4e2a<code>hide<\/code>\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u5e94\u4ed8\uff0c\u518d\u6765\u4e2a<code>show<\/code>\u65b9\u6cd5\uff0c\u5c82\u4e0d\u662f\u8fd8\u8981\u5faa\u73af\u904d\u5386\u4e00\u6b21\uff0c\u5c82\u4e0d\u662f\u8981\u70e6\u6b7b~ <img decoding=\"async\" src=\"http:\/\/mat1.gtimg.com\/www\/mb\/images\/face\/18.gif\" align=\"absmiddle\"><\/p>\n<p>\u56e0\u6b64\uff0c\u6025\u9700\u4e00\u4e2a\u904d\u5386\u5305\u88c5\u5668\u5143\u7d20\u7684\u65b9\u6cd5\uff0c\u59d1\u4e14\u53eb\u505a<code>each<\/code>\u5427~<\/p>\n<p>\u4e8e\u662f\u6709\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>F.prototype<span style=\"color:#cd0000;\">.each<\/span> = function(fn) {\n    var i=0, length = this.element.length;\n    for (; i&lt;length; i+=1) {\n        fn.call(this.element[i], i, this.element[i]);\n    }\n    return this;\n};\nF.prototype.hide = function() {\n    this<span style=\"color:#cd0000;\">.each<\/span>(function() {\n       this.style.display = \"none\";\n    });\n};\n\n$(\"img\").hide();  <span style=\"color:green;\">\/\/ \u9875\u9762\u6240\u6709\u56fe\u7247\u90fd\u9690\u85cf\u5566\uff01<\/span><\/pre>\n<\/div>\n<p>\u4e0a\u9762\u4ee3\u7801\u7684<a href=\"http:\/\/www.zhangxinxu.com\/study\/201307\/image-hide6.html\" class=\"a_link\" target=\"_blank\">demo\u5730\u5740<\/a>\u5e94\u8be5\u4e0d\u4f1a\u88ab\u4eba\u770b\u5230\u5427\u2026\u2026<\/p>\n<p><strong>9. \u6211\u4e0d\u559c\u6b22this.element, \u53ef\u4ee5\u53bb\u6389\u5417\uff1f<\/strong><br \/>\n\u73b0\u5728\u5305\u88c5\u5668\u5bf9\u8c61\u7ed3\u6784\u7c7b\u4f3c\u8fd9\u6837\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>F.prototype = {\n    element: [NodeList],\n    each: function() {},\n    hide: function() {}\n}<\/pre>\n<\/div>\n<p><code>element<\/code>\u770b\u4e0a\u53bb\u597d\u788d\u773c\uff0c\u5c31\u4e0d\u80fd\u53bb\u6389\u5417\uff1f\u53ef\u4ee5\u554a\uff0c\u5b9d\u8d1d\uff0c<code>NodeList<\/code>\u662f\u4e2a\u7c7b\u6570\u7ec4\u7ed3\u6784\uff0c\u6211\u4eec\u628a\u5b83\u4ee5\u6570\u503c\u7d22\u5f15\u5f62\u5f0f\u5206\u914d\u5230\u5bf9\u8c61\u4e2d\u5c31\u597d\u5566\uff01\u4e00\u6765\u53bb\u9664\u5197\u4f59<code>element<\/code>\u5c5e\u6027\uff0c\u4e8c\u6765\u8ba9\u539f\u578b\u5bf9\u8c61\u6210\u4e3a\u7c7b\u6570\u7ec4\u7ed3\u6784\uff0c\u53ef\u4ee5\u6709\u4e00\u4e9b\u7279\u6b8a\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4e8e\u662f\uff0c<code>F.prototype.getNodeList<\/code>\u9700\u8981\u6362\u4e00\u4e2a\u540d\u5b57\u4e86\uff0c\u6bd4\u65b9\u8bf4\u521d\u59cb\u5316<code>init<\/code>, \u4e8e\u662f\u6709\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>F.prototype<span style=\"color:#cd0000;\">.init<\/span> = function(selector, context) {\n    var nodeList = (context || document).querySelectorAll(selector);\n    <span style=\"color:#cd0000;\">this.length<\/span> = nodeList.length;\n    for (var i=0; i&lt;this.length; i+=1) {\n        <span style=\"color:#cd0000;\">this[i]<\/span> = nodeList[i];    \n    }\n    return this;\n};<\/pre>\n<\/div>\n<p>\u6b64\u65f6\uff0c<code>each<\/code>\u65b9\u6cd5\u4e2d\uff0c\u5c31\u6ca1\u6709\u70e6\u4eba\u788d\u773c\u7684<code>this.element[i]<\/code>\u51fa\u73b0\u4e86\uff0c\u800c\u662f\u76f4\u63a5\u7684<code>this[i]<\/code>. <\/p>\n<div class=\"zxx_code\">\n<pre>F.prototype.each = function(fn) {\n    var i=0, length = <span style=\"color:#cd0000;\">this.length<\/span>;\n    for (; i&lt;length; i+=1) {\n        fn.call(<span style=\"color:#cd0000;\">this[i]<\/span>, i, <span style=\"color:#cd0000;\">this[i]<\/span>);\n    }\n    return this;\n};<\/pre>\n<\/div>\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u5305\u88c5\u5668\u4e2d\u7684DOM\u5143\u7d20\u3002\u4f8b\u5982\uff1a<code>$(\"img\")[0]<\/code>\u5c31\u662f\u7b2c\u4e00\u5f20\u56fe\u7247\u5566\uff01<\/p>\n<p>\u4e0a\u9762\u4ee3\u7801\u7684<a href=\"http:\/\/www.zhangxinxu.com\/study\/201307\/image-hide7.html\" class=\"a_link\" target=\"_blank\">demo\u5730\u5740<\/a>\u5e94\u8be5\u4e0d\u4f1a\u88ab\u4eba\u770b\u5230\u5427\u2026\u2026<\/p>\n<p><strong>10. \u6211\u662f\u5b8c\u7f8e\u4e3b\u4e49\u8005\uff0c\u6211\u7279\u4e0d\u559c\u6b22F\u540d\u79f0\uff0c\u53ef\u4ee5\u6362\u6389\u5417\uff1f<\/strong><br \/>\n<code>F<\/code>\u8fd9\u4e2a\u540d\u79f0\u4ece\u5934\u5230\u5c3e\u51fa\u73b0\uff0c\u6211\u597d\u4e0d\u559c\u6b22\u7684\u6765\uff0c\u6211\u8981\u6362\u6210<code>$<\/code>, \u6211\u5c31\u662f\u8981\u6362\u6210<code>$<\/code>\u7b26\u53f7\u2026\u2026<\/p>\n<p>\u8fd9\u4e2a\u2026\u2026<code>$<\/code>\u5df2\u7ecf\u7528\u4e86\u554a\uff0c\u518d\u7528\u51b2\u7a81\u7684\u5427\u3002\u518d\u8bf4\uff0c\u4f60\u53c8\u4e0d\u662f\u72d0\u540e\uff0c\u800d\u65e0\u8d56\u4e5f\u6ca1\u7528\u554a\u2026\u2026<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201307\/shuawulai.jpg\" width=\"250\" height=\"190\" alt=\"\u72d0\u540e\u800d\u65e0\u8d56\" class=\"alignnone\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201307\/fahuo.jpg\" width=\"290\" height=\"301\" alt=\"\u5fa1\u59d0\u53d1\u98d9\u4e86\" class=\"alignnone\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/mat1.gtimg.com\/www\/mb\/images\/face\/26.gif\" align=\"absmiddle\"> \u597d\u5427\uff0c\u60f3\u60f3\u5176\u4ed6\u529e\u6cd5\u5427\u3002\u4e00\u6b65\u4e00\u6b65\u6765\uff0c\u90a3\u6211\u628a\u6240\u6709\u7684<code>F<\/code>\u6362\u6210<code>$.fn<\/code>.<\/p>\n<p>\u5c31\u6709\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201307\/2013-07-17_211826.png\" width=\"465\" height=\"439\" alt=\"\u6240\u6709\u7684F\u6362\u6210$.fn\u4e4b\u540e~\" class=\"alignnone\" \/><\/p>\n<p>\u4e0a\u56fe\u4ee3\u7801\u7684<a href=\"http:\/\/www.zhangxinxu.com\/study\/201307\/image-hide7.html\" class=\"a_link\" target=\"_blank\">demo\u5730\u5740<\/a>\u5e94\u8be5\u4e0d\u4f1a\u88ab\u4eba\u770b\u5230\u5427\u2026\u2026<\/p>\n<p>\u663e\u7136\uff0c\u8fd0\u884c\u662fOK\u7684\u3002\u4f3c\u4e4e\u4e5f\u975e\u5e38\u6709jQuery\u7684\u6a21\u6837\u4e86\uff0c\u4f46\u662f\uff0c\u5b9e\u9645\u4e0a\uff0c\u8ddfjQuery\u6bd4\u8fd8\u662f\u6709\u5dee\u522b\u7684\uff0c\u6709\u4e2a\u8f83\u5927\u7684\u5dee\u522b\u3002\u5982\u679c\u662f\u4e0a\u56fe\u4ee3\u7801\u6240\u793a\u7684JS\u7ed3\u6784\uff0c\u5219\u5305\u88c5\u5668\u5bf9\u8c61\u8981\u6269\u5c55\u65b0\u65b9\u6cd5\uff0c\u6bcf\u4e2a\u90fd\u9700\u8981\u518d\u5199\u4e00\u4e2a\u539f\u578b\u7684\u3002\u4f8b\u5982\uff0c\u6269\u5c55\u4e00\u4e2a<code>attr<\/code>\u65b9\u6cd5\uff0c\u5219\u8981\u5199\u6210\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>$.fn.prototype.attr = function() {\n    <span style=\"color:green;\">\/\/ ...<\/span>\n};<\/pre>\n<\/div>\n<p>\u53c8\u770b\u5230<code>prototype<\/code>\u4e86\uff0c\u9ad8\u7ea7\u7684\u4e1c\u897f\u5e94\u8be5\u8981\u9690\u85cf\u4f4f\uff0c\u5426\u5219\u4f1a\u7ed9\u4eba\u96be\u4ee5\u4e0a\u624b\u7684\u611f\u89c9\u3002\u90a3\u8be5\u600e\u4e48\u529e\u5462\uff1f\u5fa1\u59d0\u4e0d\u662f\u597d\u60f9\u7684\u3002<\/p>\n<p>\u8111\u5b50\u52a8\u4e00\u4e0b\u5c31\u77e5\u9053\u4e86\uff0c\u628a<code>F.prototype<\/code>\u6362\u6210<code>$.fn<\/code>\u4e0d\u4e45\u597d\u4e86\u3002\u8fd9\u6837\uff0c\u6269\u5c55\u65b0\u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u5c31\u662f<\/p>\n<div class=\"zxx_code\">\n<pre>$.fn.attr = function() {\n    <span style=\"color:green;\">\/\/ ...<\/span>\n};<pre><\/div>\n\n\u81f3\u6b64\uff0c\u5c31\u4f7f\u7528\u4e0a\u8bb2\uff0c\u4e0ejQuery\u975e\u5e38\u63a5\u8fd1\u4e86\u3002\n\n\u4f46\u662f\uff0c\u8fd8\u6709\u51e0\u4e2a<code>F<\/code>\u600e\u4e48\u529e\u5462\uff0c\u603b\u4e0d\u80fd\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\u653e\u7740\u5427\uff1a\n<div class=\"zxx_code\"><pre>var $ = function(selector, context) {\n    return new F(selector, context);\n};\nvar F = function(selector, context) {\n    return this.init(selector, context);\n};\n\n$.fn = F.prototype;\n\n$.fn.init = function(selector, context) {\n    <span style=\"color:green;\">\/\/ ...<\/span>\n    return this;\n};\n$.fn.each = function(fn) {\n   <span style=\"color:green;\">\/\/ ...<\/span>\n};\n$.fn.hide = function() {\n   <span style=\"color:green;\">\/\/ ...<\/span>\n};<\/pre>\n<\/div>\n<p>\u6570\u5b66\u4e2d\uff0c\u6211\u4eec\u90fd\u5b66\u8fc7\u5408\u5e76\u540c\u7c7b\u9879\u3002\u4ed4\u7ec6\u89c2\u5bdf\u4e0a\u9762\u7684\u7684\u4ee3\u7801\uff1a<br \/>\n<code>$()<\/code>\u8fd4\u56de\u7684\u662f<code>new F()<\/code>\uff0c\u800c<code>new F()<\/code>\u53c8\u662f\u8fd4\u56de\u7684\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u64e6\uff0c\u8fd9\u8fd4\u56de\u6765\u8fd4\u56de\u53bb\u7684\uff0c\u53c2\u6570\u53c8\u662f\u4e00\u6837\u7684\uff0c\u6211\u4eec\u662f\u4e0d\u662f\u53ef\u4ee5\u4e00\u6b21\u6027\u8fd4\u56de\uff0c\u7136\u540e\u518d\u505a\u4e9b\u624b\u811a\uff0c\u8ba9<code>$.fn.init<\/code>\u8fd4\u56de\u7684<code>this<\/code>\u4f9d\u7136\u80fd\u591f\u6b63\u786e\u6307\u5411\u3002<\/p>\n<p>\u4e8e\u662f\uff0c\u4e00\u756a\u8c03\u6574\u6709\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var $ = function(selector, context) {\n    return new $.fn.init(selector, context);\n};\nvar F = function() { };\n\n$.fn = F.prototype;\n$.fn.init = function(selector, context) {\n    <span style=\"color:green;\">\/\/ ...<\/span>\n    return this;\n};\n\n<span style=\"color:green;\">\/\/ ...<\/span><\/pre>\n<\/div>\n<p>\u4e0a\u9762\u4ee3\u7801\u663e\u7136\u662f\u6709\u95ee\u9898\u7684\uff0c<code>new<\/code>\u7684\u662f<code>$.fn.init<\/code>, <code>$.fn.init<\/code>\u7684\u8fd4\u56de\u503c\u662f<code>this<\/code>. \u4e5f\u5c31\u662f<code>$()<\/code>\u7684\u8fd4\u56de\u503c\u662f<code>$.fn.init<\/code>\u7684\u539f\u578b\u5bf9\u8c61\uff0c\u5c3c\u739b<code>$.fn.init<\/code>\u7684<code>prototype<\/code>\u539f\u578b\u73b0\u5728\u5c31\u662f\u4e2a\u5149\u6746\u53f8\u4ee4\u554a\uff0c\u54df\uff0c\u6b63\u597d\uff0c<code>$.fn<\/code>\u5bf9\u5e94\u7684\u539f\u578b\u65b9\u6cd5\uff0c\u9664\u4e86init\u6ca1\u7528\u5916\uff0c\u5176\u4ed6<code>hide()<\/code>, <code>each()<\/code>\u5c31\u662f\u6211\u4eec\u9700\u8981\u7684\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u52a0\u4e0a\u8fd9\u4e48\u4e00\u884c\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre style=\"color:#cd0000;\">$.fn.init.prototype = $.fn<\/pre>\n<\/div>\n<p>\u4e8e\u662f\uff0c<code>$()<\/code>\u7684\u8fd4\u56de\u503c\u4ece<code>$.fn.init.prototype<\/code>\u4e00\u4e0b\u5b50\u53d8\u6210<code>$.fn<\/code>\uff0c\u6b63\u597d\u5c31\u662f\u6211\u4eec\u4e00\u5f00\u59cb\u7684\u6269\u5c55\u65b9\u6cd5\u3002<\/p>\n<p>\u4e8e\u662f\u4e4e\uff0c\u5927\u529f\u544a\u6210\u3002\u6162\u7740\u2026\u2026<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201307\/dengyixia.jpg\" width=\"320\" height=\"242\" alt=\"\u6162\u7740\u2026\u2026\" class=\"mt10\" \/><\/p>\n<p>\u4e0a\u9762\u660e\u660e\u8fd8\u6709\u6b8b\u7559\u7684<code>F<\/code>\u5462\uff01<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/mat1.gtimg.com\/www\/mb\/images\/face\/22.gif\" align=\"absmiddle\"> \u54e6\uff0c\u90a3\u4e2a\u554a\u3002<code>F<\/code>\u662f\u4efb\u610f\u51fd\u6570\uff0c<code>$<\/code>\u672c\u8eab\u5c31\u662f\u51fd\u6570\uff0c\u56e0\u6b64\uff0c\u76f4\u63a5\u4f7f\u7528<code>$<\/code>\u66ff\u6362\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>var $ = function(selector, context) {\n    return new $.fn.init(selector, context);\n};\n<del datetime=\"2013-07-17T03:12:44+00:00\">var F = function() { };<\/del>   \/\/ \u8fd9\u4e2a\u76f4\u63a5\u5220\u9664\n$.fn = <span style=\"color:#cd0000;\">$<\/span>.prototype;\n$.fn.init = function(selector, context) {\n    <span style=\"color:green;\">\/\/ ...<\/span>\n    return this;\n};\n\n<span style=\"color:green;\">\/\/ ...<\/span><\/pre>\n<\/div>\n<p>\u4e0a\u56fe\u4ee3\u7801\u7684<a href=\"http:\/\/www.zhangxinxu.com\/study\/201307\/image-hide9.html\" class=\"a_link\" target=\"_blank\">demo\u5730\u5740<\/a>\u5e94\u8be5\u4e0d\u4f1a\u88ab\u4eba\u770b\u5230\u5427\u2026\u2026<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u5982\u679c\u4f60\u4e0d\u662f\u975e\u5f97\u4e00\u4e2a<code>$<\/code>\u884c\u4fbf\u5929\u4e0b\u7684\u8bdd\uff0c\u5230\u4e86\u4e0a\u9762\u8fdb\u9636\u7b2c9\u6b65\u5c31\u8db3\u591f\u4e86\u3002jQuery\u5728\u7b2c10\u6b65\u7684\u5904\u7406\u662f\u4e3a\u4e86\u5f70\u663e\u5176<code>$<\/code>\u7528\u5f97\u5982\u6b64\u7684\u51fa\u795e\u5165\u5316\uff0c\u4ee3\u7801\u5b8c\u7f8e\uff0c\u4ee4\u4eba\u60ca\u53f9\uff01<\/p>\n<p>\u81f3\u6b64\uff0cjQuery\u5927\u6838\u5fc3\u5df2\u7ecf\u4e00\u6b65\u4e00\u6b65\u8d70\u5b8c\u4e86\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u6240\u6709\u7684\u8fd9\u4e9b\u8fdb\u9636\u90fd\u662f\u6839\u636e\u9700\u6c42\u3001\u5b9e\u9645\u5f00\u53d1\u9700\u8981\u6765\u7684\uff0c\u6162\u6162\u5b8c\u5584\uff0c\u6162\u6162\u6269\u5145\u7684\uff01<\/p>\n<p><strong>11. \u6bcf\u4e2a\u6269\u5c55\u65b9\u6cd5\u90fd\u8981$.fn.xxxx, \u597d\u95f9\u5fc3\u7684\u6765<\/strong><\/p>\n<div class=\"zxx_code\">\n<pre>$.fn.css = function() {}\n$.fn.attr = function() {}\n$.fn.data = function() {}\n<span style=\"color:green;\">\/\/ ...<\/span><\/pre>\n<\/div>\n<p>\u6bcf\u4e2a\u6269\u5c55\u524d\u9762\u90fd\u6709\u4e2a<code>$.fn<\/code>, \u597d\u8ba8\u538c\u7684\u611f\u89c9\uff0c\u5c31\u4e0d\u80fd\u5408\u5e76\u5417\uff1f<\/p>\n<p>\u4e8e\u662f\uff0cjQuery\u641e\u4e86\u4e2a<code>extend<\/code>\u65b9\u6cd5\u3002<\/p>\n<div class=\"zxx_code\">\n<pre>$.fn.extend({\n    css: function() {},\n    attr: function() {},\n    data: function() {},\n    <span style=\"color:green;\">\/\/ ...<\/span>\n});<\/pre>\n<\/div>\n<p><strong>12. $()\u4e0d\u4ec5\u53ef\u4ee5\u662f\u9009\u62e9\u5668\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u662fDOM<\/strong><br \/>\n\u5728<code>init<\/code>\u65b9\u6cd5\u4e2d\uff0c\u5224\u65ad\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u5982\u679c\u662f\u8282\u70b9\uff0c\u76f4\u63a5<code>this[0] = this_node<\/code>. over!<\/p>\n<p>\u4ee5\u4e0b13~?\u90fd\u662f\u5b8c\u5584\u554a\uff0c\u8865\u5145\u554a\uff0c\u517c\u5bb9\u6027\u5904\u7406\u554a\u4ec0\u4e48\u7684\uff0c\u6ca1\u6709\u4ef7\u503c\uff0c\u5230\u6b64\u4e3a\u6b62\uff01<\/p>\n<h3>\u4e09\u3001\u6392\u4e86\u5f88\u957f\u961f\u7684\u7ed3\u675f\u8bed<\/h3>\n<p>\u7f51\u4e0a\u4e5f\u6709\u5176\u4ed6\u4e00\u4e9b\u4ecb\u7ecdjQuery\u539f\u7406\u6216\u673a\u5236\u7684\u6587\u7ae0\uff0c\u53ef\u80fd\u5f53\u4e8b\u4eba\u81ea\u5df1\u7406\u89e3\uff0c\u800c\u9605\u8bfb\u8005\u672c\u6765\u5c31\u4e0d\u61c2\uff0c\u8bf4\u6765\u8bf4\u53bb\uff0c\u8d8a\u8bf4\u8d8a\u7ed5\uff0c\u53ef\u80fd\u66f4\u4e0d\u61c2\u4e86\u3002<\/p>\n<p>jQuery\u662f\u5f88\u4f18\u79c0\uff0c\u597d\u6bd4\u8eab\u4e3a\u7075\u957f\u7c7b\u7684\u4eba\u7c7b\u3002\u4f46\u662f\uff0c\u5176\u8bde\u751f\u663e\u7136\u662f\u4ece\u7b80\u5355\u5f00\u59cb\u7684\u3002\u56e0\u6b64\uff0c\u8981\u4e86\u89e3\u4eba\u7c7b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8ffd\u6eaf\u5176\u8d77\u6e90\u3002\u5982\u679c\u4f60\u662f\u4e0a\u5e1d\uff0c\u8981\u8ba9\u4f60\u9020\u4e00\u4e2a\u4eba\uff0c\u4f60\u4f1a\u600e\u4e48\u9020\uff0c\u662f\u4e00\u53e3\u6c14\u51fa\u6765\uff1f\u5973\u5a32\u9020\u4eba\u8fd8\u8981\u634f\u6ce5\u4eba\u5462\uff01\u4e0d\u59a8\u4ece\u5355\u7ec6\u80de\u751f\u7269\u5f00\u59cb\uff0c\u968f\u7740\u81ea\u7136\u8fdb\u5316\uff0c\u6dd8\u6c70\uff0c\u81ea\u7136\u800c\u7136\uff0c\u5c31\u4f1a\u51fa\u73b0\u4eba\u7c7b\uff0c\u4e0a\u5e1d\u4ed6\u5c31\u662f\u8fd9\u4e48\u5e72\u7684\u3002<\/p>\n<p>jQuery\u7684\u8bde\u751f\u4e5f\u5927\u81f4\u5982\u6b64\uff0c\u8981\u60f3\u4e86\u89e3jQuery\uff0c\u53ef\u4ee5\u8bd5\u8bd5\u8e0f\u7740\u672c\u6587jQuery\u7684\u6210\u957f\u8db3\u8ff9\uff0c\u4e00\u70b9\u4e00\u70b9\u9010\u6b65\u6df1\u5165\uff0c\u60a8\u5c31\u4f1a\u4e86\u89e3\u4e3a\u4f55jQuery\u8981\u8fd9\u4e48\u8bbe\u8ba1\uff0c\u5b83\u662f\u5982\u4f55\u8bbe\u8ba1\u7684\u7b49\u3002<\/p>\n<p>\u867d\u7136\uff0c\u5185\u5bb9\u7531\u6d45\u53ca\u6df1\uff0c\u4f46\u662f\uff0c\u5176\u4e2d\u6d89\u53ca\u7684\u539f\u578b\u4ee5\u53ca<code>new<\/code>\u6784\u9020\u51fd\u6570\u7684\u4e00\u4e9b\u7279\u6027\uff0c\u5bf9\u4e8e\u65b0\u4eba\u800c\u8a00\uff0c\u8fd8\u662f\u6709\u4e00\u4e9b\u7406\u89e3\u95e8\u69db\u7684\uff0c\u5e0c\u671b\u6211\u7684\u63cf\u8ff0\u4e0e\u89e3\u91ca\u53ef\u4ee5\u8ba9\u4f60\u6709\u4e00\u4e1d\u8c41\u7136\u5f00\u6717\uff0c\u90a3\u5c31\u518d\u597d\u4e0d\u8fc7\u4e86\u3002<\/p>\n<p>\u611f\u8c22\u60a8\u7684\u9605\u8bfb\u81f3\u6b64\uff0c\u6b22\u8fce\u6307\u51fa\u6587\u7ae0\u53ef\u80fd\u4e66\u5199\u4e0d\u51c6\u786e\u7684\u5730\u65b9\uff0c\u518d\u6b21\u611f\u8c22\uff01<\/p>\n<p>\u6708\u5e95\u5728\u767e\u59d3\u7f51\u6709\u4e2a\u5c0f\u5206\u4eab\uff0c\u6f14\u793a\u6587\u6863\u8fde\u4e2a\u8089\u6e23\u5b50\u8fd8\u6ca1\u51c6\u5907\u5462\u3002\u56e0\u6b64\uff0c\u672a\u6765\u4e00\u5468\u4f11\u6587\u3002<\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u6765\u81ea<a href=\"http:\/\/www.zhangxinxu.com\/\">\u5f20\u946b\u65ed-\u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<\/a>[<a href=\"http:\/\/www.zhangxinxu.com\/\">http:\/\/www.zhangxinxu.com<\/a>]<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=3520\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=3520<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=3520\" ttile=\"\u70b9\u51fb\u67e5\u770b\u672c\u6587\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201307\/jquery-born.jpg\" width=\"550\" height=\"170\" class=\"imgpad\"><\/a><\/p>\n<p>\u7f51\u4e0a\u4e5f\u6709\u5176\u4ed6\u4e00\u4e9b\u4ecb\u7ecdjQuery\u539f\u7406\u6216\u673a\u5236\u7684\u6587\u7ae0\uff0c\u53ef\u80fd\u5f53\u4e8b\u4eba\u81ea\u5df1\u7406\u89e3\uff0c\u800c\u9605\u8bfb\u8005\u672c\u6765\u5c31\u4e0d\u61c2\uff0c\u8bf4\u6765\u8bf4\u53bb\uff0c\u8d8a\u8bf4\u8d8a\u7ed5\uff0c\u53ef\u80fd\u66f4\u4e0d\u61c2\u4e86\u3002<\/p>\n<p>jQuery\u662f\u5f88\u4f18\u79c0\uff0c\u597d\u6bd4\u8eab\u4e3a\u7075\u957f\u7c7b\u7684\u4eba\u7c7b\u3002\u4f46\u662f\uff0c\u5176\u8bde\u751f\u663e\u7136\u662f\u4ece\u7b80\u5355\u5f00\u59cb\u7684\u3002\u56e0\u6b64\uff0c\u8981\u4e86\u89e3\u4eba\u7c7b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8ffd\u6eaf\u5176\u8d77\u6e90\u3002\u5982\u679c\u4f60\u662f\u4e0a\u5e1d\uff0c\u8981\u8ba9\u4f60\u9020\u4e00\u4e2a\u4eba\uff0c\u4f60\u4f1a\u600e\u4e48\u9020\uff0c\u662f\u4e00\u53e3\u6c14\u51fa\u6765\uff1f\u5973\u5a32\u9020\u4eba\u8fd8\u8981\u634f\u6ce5\u4eba\u5462\uff01\u4e0d\u59a8\u4ece\u5355\u7ec6\u80de\u751f\u7269\u5f00\u59cb\uff0c\u968f\u7740\u81ea\u7136\u8fdb\u5316\uff0c\u6dd8\u6c70\uff0c\u81ea\u7136\u800c\u7136\uff0c\u5c31\u4f1a\u51fa\u73b0\u4eba\u7c7b\uff0c\u4e0a\u5e1d\u4ed6\u5c31\u662f\u8fd9\u4e48\u5e72\u7684\u3002<\/p>\n<p>jQuery\u7684\u8bde\u751f\u4e5f\u5927\u81f4\u5982\u6b64\uff0c\u8981\u60f3\u4e86\u89e3jQuery\uff0c\u53ef\u4ee5\u8bd5\u8bd5\u8e0f\u7740\u672c\u6587jQuery\u7684\u6210\u957f\u8db3\u8ff9\uff0c\u4e00\u70b9\u4e00\u70b9\u9010\u6b65\u6df1\u5165\uff0c\u60a8\u5c31\u4f1a\u4e86\u89e3\u4e3a\u4f55jQuery\u8981\u8fd9\u4e48\u8bbe\u8ba1\uff0c\u5b83\u662f\u5982\u4f55\u8bbe\u8ba1\u7684\u7b49\u3002<\/p>\n<p>\u867d\u7136\uff0c\u5185\u5bb9\u7531\u6d45\u53ca\u6df1\uff0c\u4f46\u662f\uff0c\u5176\u4e2d\u6d89\u53ca\u7684\u539f\u578b\u4ee5\u53ca<code>new<\/code>\u6784\u9020\u51fd\u6570\u7684\u4e00\u4e9b\u7279\u6027\uff0c\u5bf9\u4e8e\u65b0\u4eba\u800c\u8a00\uff0c\u8fd8\u662f\u6709\u4e00\u4e9b\u7406\u89e3\u95e8\u69db\u7684\uff0c\u5e0c\u671b\u6211\u7684\u63cf\u8ff0\u4e0e\u89e3\u91ca\u53ef\u4ee5\u8ba9\u4f60\u6709\u4e00\u4e1d\u8c41\u7136\u5f00\u6717\uff0c\u90a3\u5c31\u518d\u597d\u4e0d\u8fc7\u4e86\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[182,451,14,775,568,758,508,449],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/3520"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=3520"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/3520\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=3520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=3520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=3520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}