{"id":168476,"date":"2024-05-08T16:24:24","date_gmt":"2024-05-08T08:24:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/168476.html"},"modified":"2024-05-08T16:24:30","modified_gmt":"2024-05-08T08:24:30","slug":"javascript-%e6%80%8e%e4%b9%88%e7%bb%99jquery%e5%b0%81%e8%a3%85%e4%b8%8a%e4%b8%80%e4%b8%aa%e6%96%b0%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/168476.html","title":{"rendered":"JavaScript \u600e\u4e48\u7ed9jQuery\u5c01\u88c5\u4e0a\u4e00\u4e2a\u65b0\u65b9\u6cd5"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27034654\/f4fdf09a-6de2-4900-8bb8-a46c1d0246bd.webp\" alt=\"JavaScript \u600e\u4e48\u7ed9jQuery\u5c01\u88c5\u4e0a\u4e00\u4e2a\u65b0\u65b9\u6cd5\" \/><\/p>\n<p><p><strong>JavaScript \u7ed9jQuery\u5c01\u88c5\u4e00\u4e2a\u65b0\u65b9\u6cd5<\/strong>\uff0c\u9700\u8981\u4f7f\u7528<code>jQuery.fn.extend()<\/code>\u51fd\u6570\u6216\u76f4\u63a5\u5728<code>jQuery.fn<\/code>\u5bf9\u8c61\u4e0a\u6dfb\u52a0\u65b9\u6cd5\u3002<strong>\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fd\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u6269\u5c55jQuery\u7684\u539f\u578b<\/strong>\uff0c\u4ece\u800c\u6240\u6709\u7684jQuery\u5bf9\u8c61\u90fd\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e9b\u65b0\u7684\u65b9\u6cd5\u3002\u5c55\u5f00\u63cf\u8ff0\u7b2c\u4e00\u79cd\u65b9\u5f0f\uff0c<code>jQuery.fn.extend()<\/code>\u662f\u4e00\u4e2a\u516c\u5171\u7684\u6269\u5c55\u70b9\uff0c\u7528\u4e8e\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u65b9\u6cd5\uff0c\u5176\u8bed\u6cd5\u7ed3\u6784\u6e05\u6670\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001JQUERY.FN.EXTEND()<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>jQuery.fn.extend()<\/code>\u53ef\u4ee5\u4e00\u6b21\u6027\u5411jQuery\u539f\u578b\u6dfb\u52a0\u591a\u4e2a\u65b9\u6cd5\u3002\u8fd9\u662f\u4e00\u4e2a\u6279\u91cf\u64cd\u4f5c\uff0c\u7279\u522b\u9002\u5408\u9700\u8981\u6dfb\u52a0\u591a\u4e2a\u63d2\u4ef6\u65b9\u6cd5\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>\u8bed\u6cd5\u7ed3\u6784<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">jQuery.fn.extend({<\/p>\n<p>    newMethod1: function() {<\/p>\n<p>        \/\/ \u65b9\u6cd5\u5b9e\u73b0<\/p>\n<p>    },<\/p>\n<p>    newMethod2: function() {<\/p>\n<p>        \/\/ \u65b9\u6cd5\u5b9e\u73b0<\/p>\n<p>    }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">jQuery.fn.extend({<\/p>\n<p>    highlight: function(color) {<\/p>\n<p>        return this.css(&#039;backgroundColor&#039;, color);<\/p>\n<p>    },<\/p>\n<p>    displayAlert: function(message) {<\/p>\n<p>        return this.each(function() {<\/p>\n<p>            alert(message);<\/p>\n<p>        });<\/p>\n<p>    }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>highlight<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4efb\u4f55jQuery\u5bf9\u8c61\u7684\u80cc\u666f\u989c\u8272\u6539\u53d8\uff0c\u5e76\u4e14<code>displayAlert<\/code>\u4e3a\u6bcf\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u5f39\u51fa\u4e00\u4e2a\u8b66\u544a\u6846\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u76f4\u63a5\u5728JQUERY.FN\u4e0a\u6dfb\u52a0\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u7ed9jQuery\u5c01\u88c5\u65b0\u65b9\u6cd5\u7684\u65b9\u5f0f\u662f\u76f4\u63a5\u5728<code>jQuery.fn<\/code>\u5bf9\u8c61\u4e0a\u6dfb\u52a0\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u6bd4\u8f83\u76f4\u63a5\uff0c\u9002\u5408\u6dfb\u52a0\u5355\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u8bed\u6cd5\u7ed3\u6784<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">jQuery.fn.newMethod = function() {<\/p>\n<p>    \/\/ \u65b9\u6cd5\u5b9e\u73b0<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">jQuery.fn.stripedTable = function() {<\/p>\n<p>    return this.find(&#039;tbody tr:odd&#039;).css(&#039;background-color&#039;, &#039;#f2f2f2&#039;);<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>stripedTable<\/code>\u65b9\u6cd5\uff0c\u5b83\u4f1a\u4e3a\u8868\u683c\u7684\u5947\u6570\u884c\u8bbe\u7f6e\u6591\u9a6c\u7ebf\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528YOUR PLUGIN\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5f00\u53d1\u8005\u901a\u5e38\u4f1a\u7ed9\u5176\u63d2\u4ef6\u547d\u540d\uff0c\u4f60\u53ef\u4ee5\u5c06\u81ea\u5b9a\u4e49\u7684\u65b9\u6cd5\u653e\u5165\u4e00\u4e2a\u72ec\u7acb\u7684\u540d\u79f0\u7a7a\u95f4\u4e2d\uff0c\u4f7f\u4e4b\u66f4\u52a0\u7ed3\u6784\u5316\u3002<\/p>\n<\/p>\n<p><h4>\u8bed\u6cd5\u7ed3\u6784<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">(function($){<\/p>\n<p>    $.fn.yourPlugin = {<\/p>\n<p>        newMethod1: function() {<\/p>\n<p>            \/\/ \u65b9\u6cd5\u5b9e\u73b0<\/p>\n<p>        },<\/p>\n<p>        newMethod2: function() {<\/p>\n<p>            \/\/ \u65b9\u6cd5\u5b9e\u73b0<\/p>\n<p>        }<\/p>\n<p>    };<\/p>\n<p>})(jQuery);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">;(function($){<\/p>\n<p>    $.fn.myPlugin = function(options) {<\/p>\n<p>        var settings = $.extend({<\/p>\n<p>            color: &#039;red&#039;,<\/p>\n<p>            backgroundColor: &#039;yellow&#039;<\/p>\n<p>        }, options );<\/p>\n<p>        return this.css({<\/p>\n<p>            color: settings.color,<\/p>\n<p>            backgroundColor: settings.backgroundColor<\/p>\n<p>        });<\/p>\n<p>    };<\/p>\n<p>})(jQuery);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>myPlugin<\/code>\u65b9\u6cd5\u65f6\uff0c\u4f60\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u5305\u542b<code>color<\/code>\u548c<code>backgroundColor<\/code>\u5c5e\u6027\u7684\u5bf9\u8c61\uff0c\u4ee5\u5b9a\u5236\u5316\u5e94\u7528\u63d2\u4ef6\u7684\u989c\u8272\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5c01\u88c5\u65b9\u6cd5\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5c01\u88c5\u65b0\u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u6709\u51e0\u70b9\u9700\u8981\u7279\u522b\u6ce8\u610f\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u94fe\u5f0f\u8c03\u7528<\/strong>\uff1a\u786e\u4fdd\u4f60\u7684\u65b9\u6cd5\u5728\u8fd4\u56de\u7ed3\u679c\u65f6\u603b\u662f\u8fd4\u56de<code>jQuery<\/code>\u5bf9\u8c61\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u6301jQuery\u65b9\u6cd5\u94fe\u5f0f\u8c03\u7528\u7684\u7279\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u4f18\u5316<\/strong>\uff1a\u8003\u8651\u5230\u6027\u80fd\uff0c\u5e94\u8be5\u5c3d\u91cf\u51cf\u5c11DOM\u64cd\u4f5c\u548c\u904d\u5386\uff0c\u5c24\u5176\u662f\u5728\u4f60\u7684\u65b9\u6cd5\u4e2d\u5305\u542b\u5927\u91cf\u8fd9\u7c7b\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u901a\u7528\u6027\u4e0e\u7075\u6d3b\u6027<\/strong>\uff1a\u5c3d\u53ef\u80fd\u8ba9\u4f60\u7684\u65b9\u6cd5\u5177\u6709\u901a\u7528\u6027\uff0c\u540c\u65f6\u63d0\u4f9b\u8db3\u591f\u7684\u914d\u7f6e\u9879\uff0c\u8ba9\u5176\u4ed6\u5f00\u53d1\u8005\u6839\u636e\u5177\u4f53\u9700\u6c42\u4fee\u6539\u884c\u4e3a\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u547d\u540d\u51b2\u7a81<\/strong>\uff1a\u4f7f\u7528\u72ec\u7279\u7684\u65b9\u6cd5\u540d\uff0c\u907f\u514d\u4e0e\u73b0\u5b58\u7684jQuery\u65b9\u6cd5\u6216\u5176\u4ed6\u63d2\u4ef6\u4ea7\u751f\u51b2\u7a81\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d2\u4ef6\u5c01\u88c5\u539f\u5219<\/strong>\uff1a\u9075\u5faa\u826f\u597d\u7684\u63d2\u4ef6\u5f00\u53d1\u89c4\u8303\uff0c\u4f8b\u5982\u4f7f\u7528\u81ea\u6267\u884c\u533f\u540d\u51fd\u6570\u5c01\u88c5\u4f60\u7684\u63d2\u4ef6\uff0c\u4ee5\u907f\u514d\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u6c61\u67d3\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u8fdb\u9636\u5c01\u88c5\u6280\u5de7 &#8211; \u7ef4\u62a4\u63d2\u4ef6\u72b6\u6001<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u7ef4\u62a4\u63d2\u4ef6\u5185\u90e8\u72b6\u6001\u6216\u63d0\u4f9b\u591a\u79cd\u884c\u4e3a\u3002\u8fd9\u65f6\uff0c\u4f60\u53ef\u4ee5\u5728\u65b9\u6cd5\u5185\u90e8\u5224\u65ad\u4f20\u5165\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u6216\u5229\u7528<code>data<\/code>\u65b9\u6cd5\u5728\u5143\u7d20\u4e0a\u5b58\u50a8\u72b6\u6001\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">$.fn.tabControl = function(action) {<\/p>\n<p>    if (action === &#039;destroy&#039;) {<\/p>\n<p>        \/\/ \u79fb\u9664\u63d2\u4ef6\u6548\u679c\u548c\u6570\u636e<\/p>\n<p>        return this.each(function() {<\/p>\n<p>            $(this).removeData(&#039;tabControl&#039;);<\/p>\n<p>        });<\/p>\n<p>    }<\/p>\n<p>    \/\/ \u63d2\u4ef6\u521d\u59cb\u5316\u4ee3\u7801<\/p>\n<p>    \/\/ ...<\/p>\n<p>    return this.each(function() {<\/p>\n<p>        var $this = $(this);<\/p>\n<p>        $this.data(&#039;tabControl&#039;, {}); \/\/ \u5b58\u653e\u63d2\u4ef6\u6570\u636e<\/p>\n<p>    });<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5982\u4f55\u7ed9jQuery\u5c01\u88c5\u4e00\u4e2a\u65b0\u7684JavaScript\u65b9\u6cd5\uff1f<\/strong><\/p>\n<p><strong>A: \u600e\u6837\u5728jQuery\u4e2d\u5c01\u88c5\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684JavaScript\u65b9\u6cd5\uff1f<\/strong><\/p>\n<p>\u5c06\u60a8\u60f3\u8981\u5c01\u88c5\u7684\u529f\u80fd\u653e\u5728\u4e00\u4e2aJavaScript\u51fd\u6570\u4e2d\uff0c\u5e76\u4f7f\u7528jQuery.fn.extend()\u65b9\u6cd5\u5c06\u5176\u6269\u5c55\u5230jQuery\u5bf9\u8c61\u4e0a\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>$.fn.myCustomMethod = function() {\n  \/\/ \u5728\u8fd9\u91cc\u7f16\u5199\u60a8\u7684\u81ea\u5b9a\u4e49\u65b9\u6cd5\u7684\u4ee3\u7801\n}\n\n\/\/ \u4f7f\u7528\u65b9\u6cd5\u793a\u4f8b\n$(&#039;.my-element&#039;).myCustomMethod();\n<\/code><\/pre>\n<p><strong>Q: \u5982\u4f55\u5c01\u88c5\u4e00\u4e2a\u80fd\u591f\u5728jQuery\u4e2d\u4f7f\u7528\u7684\u529f\u80fd\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u91cd\u7528\u6027\uff1f<\/strong><\/p>\n<p><strong>A: \u600e\u6837\u5c06\u4e00\u4e2a\u53ef\u91cd\u7528\u7684\u529f\u80fd\u5c01\u88c5\u4e3a jQuery \u63d2\u4ef6\uff1f<\/strong><\/p>\n<p>\u5c06\u529f\u80fd\u5c01\u88c5\u5230\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684jQuery\u63d2\u4ef6\u4e2d\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u91cd\u7528\u6027\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u63a5\u6536jQuery\u4f5c\u4e3a\u53c2\u6570\u3002\u7136\u540e\uff0c\u5728\u8be5\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528 jQuery.fn.extend() \u65b9\u6cd5\u5c06\u529f\u80fd\u6dfb\u52a0\u5230jQuery\u5bf9\u8c61\u539f\u578b\u4e2d\u3002\u6700\u540e\uff0c\u5c06\u63d2\u4ef6\u6dfb\u52a0\u5230\u9875\u9762\u7684script\u6807\u7b7e\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>(function($) {\n  $.fn.myCustomPlugin = function() {\n    \/\/ \u5728\u8fd9\u91cc\u7f16\u5199\u60a8\u7684\u63d2\u4ef6\u4ee3\u7801\n  }\n})(jQuery);\n\n\/\/ \u4f7f\u7528\u65b9\u6cd5\u793a\u4f8b\n$(&#039;.my-element&#039;).myCustomPlugin();\n<\/code><\/pre>\n<p><strong>Q: \u5c01\u88c5\u4e86\u4e00\u4e2a\u65b0\u7684JavaScript\u65b9\u6cd5\u540e\uff0c\u5982\u4f55\u786e\u4fdd\u8be5\u65b9\u6cd5\u7b26\u5408jQuery\u7684\u6700\u4f73\u5b9e\u8df5\uff1f<\/strong><\/p>\n<p><strong>A: \u5982\u4f55\u6839\u636ejQuery\u7684\u6700\u4f73\u5b9e\u8df5\u89c4\u8303\u5c01\u88c5\u4e00\u4e2a\u65b0\u7684JavaScript\u65b9\u6cd5\uff1f<\/strong><\/p>\n<p>\u5728\u5c01\u88c5\u65b0\u7684JavaScript\u65b9\u6cd5\u65f6\uff0c\u8981\u9075\u5faajQuery\u7684\u6700\u4f73\u5b9e\u8df5\u89c4\u8303\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u6027\u80fd\u3002\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\u4f7f\u7528\u9002\u5f53\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u907f\u514d\u5168\u5c40\u53d8\u91cf\u6c61\u67d3\uff0c\u6309\u9700\u4f7f\u7528\u94fe\u5f0f\u8c03\u7528\uff0c\u4f7f\u7528jQuery\u7684\u4ee3\u7406\u548c\u4e8b\u4ef6\u7ed1\u5b9a\u51fd\u6570\u7b49\u3002\u53ef\u4ee5\u67e5\u9605jQuery\u7684\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u6307\u5357\uff0c\u4e86\u89e3\u66f4\u591a\u5173\u4e8ejQuery\u6700\u4f73\u5b9e\u8df5\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript \u7ed9jQuery\u5c01\u88c5\u4e00\u4e2a\u65b0\u65b9\u6cd5\uff0c\u9700\u8981\u4f7f\u7528jQuery.fn.extend()\u51fd\u6570\u6216\u76f4\u63a5\u5728j [&hellip;]","protected":false},"author":3,"featured_media":168486,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/168476"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=168476"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/168476\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/168486"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=168476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=168476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=168476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}