{"id":1140,"date":"2017-07-13T10:50:22","date_gmt":"2017-07-13T02:50:22","guid":{"rendered":"http:\/\/feizhaojun.com\/?p=1140"},"modified":"2018-02-24T15:43:58","modified_gmt":"2018-02-24T07:43:58","slug":"stoppropagation%ef%bc%8cpreventdefault%e5%92%8creturn-false","status":"publish","type":"post","link":"https:\/\/feizhaojun.com\/?p=1140","title":{"rendered":"stopPropagation()\uff0cpreventDefault()\u548creturn false"},"content":{"rendered":"<p>\u9605\u8bfb\u672c\u6587\u4e4b\u524d\u4f60\u6700\u597d\u4e86\u89e3JavaScript\u5728\u6d4f\u89c8\u5668\u4e2d <strong>\u4e8b\u4ef6\u5192\u6ce1<\/strong> \u548c <strong>\u4e8b\u4ef6\u6355\u83b7<\/strong> \u4e24\u4e2a\u6982\u5ff5\u3002<\/p>\n<p>\u56e0\u4e3aHTML\u7ed3\u6784\u4e2d\u5b58\u5728\u7740\u7236\u3001\u5b50\u8282\u70b9\u7684\u4e8b\u4ef6\u5192\u6ce1\uff0c\u4ee5\u53ca\u6d4f\u89c8\u5668\u9ed8\u8ba4\u52a8\u4f5c\uff0c\u6240\u4ee5\u6211\u4eec\u4f7f\u7528 JavaScript \u65f6\u4e3a\u4e86\u8fbe\u5230\u9884\u671f\u6548\u679c\u7ecf\u5e38\u9700\u8981\u963b\u6b62\u4e8b\u4ef6\u548c\u52a8\u4f5c\u6267\u884c. \u4e00\u822c\u6211\u4eec\u4f1a\u7528\u5230\u4e09\u79cd\u65b9\u6cd5, \u5206\u522b\u662f stopPropagation()\uff0cpreventDefault() \u548c return false\u3002\u5b83\u4eec\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u8be5\u600e\u4e48\u4f7f\u7528\u5462\uff1f\u5c06\u5728\u672c\u6587\u4e2d\u8fdb\u884c\u8bb2\u89e3\u3002<\/p>\n<blockquote>\n<p><strong>\u540d\u8bcd\u89e3\u91ca<\/strong><\/p>\n<p><strong>\u76d1\u542c\u4e8b\u4ef6<\/strong>\uff1a\u6307\u5728\u8282\u70b9\u4e0a\u80fd\u88ab\u76d1\u542c\u7684\u9875\u9762\u64cd\u4f5c\u3002\u5982: select \u8282\u70b9\u7684 change \u4e8b\u4ef6, a \u8282\u70b9\u7684 click \u4e8b\u4ef6\u3002<br \/>\n<strong>\u6d4f\u89c8\u5668\u9ed8\u8ba4\u52a8\u4f5c<\/strong>\uff1a\u6307\u7279\u5b9a\u9875\u9762\u5143\u7d20\u4e0a\u5e26\u6709\u7684\u529f\u80fd\u3002\u5982: \u70b9\u51fb a \u94fe\u63a5\u8282\u70b9\u7684\u8df3\u8f6c\u52a8\u4f5c, \u8868\u5355\u63d0\u4ea4\u52a8\u4f5c\u3002<br \/>\n<strong>stopPropagation()<\/strong>\uff1a\u963b\u6b62\u4e8b\u4ef6\u5192\u6ce1\u3002<br \/>\n<strong>preventDefault()<\/strong>\uff1a\u963b\u6b62\u6d4f\u89c8\u5668\u9ed8\u8ba4\u884c\u4e3a\u3002<\/p>\n<\/blockquote>\n<p><strong>stopPropagation()<\/strong><\/p>\n<p>\u56e0\u4e3a\u4e8b\u4ef6\u53ef\u4ee5\u5728\u5404\u5c42\u7ea7\u7684\u8282\u70b9\u4e2d\u4f20\u9012,\u4e0d\u7ba1\u662f\u5192\u6ce1\u8fd8\u662f\u6355\u83b7,\u6709\u65f6\u6211\u4eec\u5e0c\u671b\u4e8b\u4ef6\u5728\u7279\u5b9a\u8282\u70b9\u6267\u884c\u5b8c\u4e4b\u540e\u4e0d\u518d\u4f20\u9012,\u53ef\u4ee5\u4f7f\u7528\u4e8b\u4ef6\u5bf9\u8c61\u7684 stopPropagation() \u65b9\u6cd5\u3002<br \/>\n\u5047\u8bbe\u9875\u9762\u4e0a\u5b58\u5728\u4e00\u4e2a\u6d6e\u52a8\u5f39\u51fa\u5c42,\u663e\u793a\u5728\u6700\u524d\u9762,\u5f53\u70b9\u51fb\u5f39\u51fa\u5c42\u4ee5\u5916\u9875\u9762\u533a\u57df\u65f6,\u9690\u85cf\u5f39\u51fa\u5c42\u3002\u4e3a\u4e86\u505a\u5230\u8fd9\u6837\u7684\u6548\u679c,\u6211\u4eec\u4f1a\u76d1\u542c documentElement \u7684 click \u4e8b\u4ef6,\u4e00\u65e6\u4e8b\u4ef6\u88ab\u89e6\u53d1\u5373\u9690\u85cf\u5f39\u51fa\u5c42\u3002\u4f46\u662f...<br \/>\n\u8fd9\u663e\u7136\u5b58\u5728\u95ee\u9898\u3002\u5f53\u7528\u6237\u70b9\u51fb\u5f39\u51fa\u5c42\u65f6,\u6211\u4eec\u4e0d\u5e0c\u671b\u5b83\u9690\u85cf\u6389\u3002\u4f46\u56e0\u4e3a\u4e8b\u4ef6\u7684\u5192\u6ce1\u4f20\u9012,documentElement \u7684 click \u4e8b\u4ef6\u4e5f\u4f1a\u88ab\u89e6\u53d1\u3002\u8fd9\u4e2a\u65f6\u5019,\u6211\u4eec\u53ef\u4ee5\u76d1\u542c\u5f39\u51fa\u5c42\u7684 click \u4e8b\u4ef6,\u5e76\u4f7f\u7528 stopPropagation() \u65b9\u6cd5\u963b\u6b62\u5192\u6ce1\u3002\u8bf7\u53c2\u8003\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<p>[code lang=\"javascript\"]<\/p>\n<p>\/\/ \u5728\u5f39\u51fa\u5bf9\u8bdd\u6846\u4e0a\u70b9\u51fb\u65f6, \u4e0d\u8fdb\u884c\u4efb\u4f55\u9875\u9762\u64cd\u4f5c, \u5e76\u963b\u6b62\u5192\u6ce1<br \/>\ndocument.getElementById('dialog').onclick = function(ev) {<br \/>\n    ev.stopPropagation();<br \/>\n};<br \/>\n\/\/ \u5728 documentElement \u8282\u70b9\u4e0a\u76d1\u542c\u5230\u70b9\u51fb\u4e8b\u4ef6\u65f6, \u9690\u85cf\u5bf9\u8bdd\u6846<br \/>\ndocument.documentElement.onclick = function(ev) {<br \/>\n    document.getElementById('dialog').style.display = 'none';<br \/>\n};<\/p>\n<p>[\/code]<\/p>\n<p>stopPropagation() \u76f8\u5f53\u597d\u7528\uff0c\u53ef\u662f IE8 \u53ca\u4ee5\u524d\u7248\u672c\u90fd\u4e0d\u652f\u6301(\u7528jquery\u5c31\u4e0d\u7528\u8003\u7565\u8fd9\u70b9\u4e86)\u3002IE \u7684\u4e8b\u4ef6\u5bf9\u8c61\u5305\u542b\u7279\u6709\u7684\u5c5e\u6027 cancelBubble, \u53ea\u8981\u5c06\u5b83\u8d4b\u503c\u4e3a false \u5373\u53ef\u963b\u6b62\u4e8b\u4ef6\u7ee7\u7eed\u3002\u5982:<\/p>\n<p>[code lang=\"javascript\"]<\/p>\n<p>\/\/ \u5728\u5f39\u51fa\u5bf9\u8bdd\u6846\u4e0a\u70b9\u51fb\u65f6, \u4e0d\u8fdb\u884c\u4efb\u4f55\u9875\u9762\u64cd\u4f5c, \u5e76\u963b\u6b62\u5192\u6ce1<br \/>\ndocument.getElementById('dialog').onclick = function(ev) {<br \/>\n    ev.cancelBubble = false;<br \/>\n};<\/p>\n<p>[\/code]<\/p>\n<p><strong>preventDefault()<\/strong><\/p>\n<p>\u4e00\u4e2a\u5e26\u4e8b\u4ef6\u76d1\u542c\u7684\u94fe\u63a5\u4ee3\u7801\u5982\u4e0b:<\/p>\n<p>[code lang=\"html\"]<\/p>\n<p>&lt;a href=&quot;http:\/\/w3c.org&quot; onclick=&quot;alert('my name is chic!');&quot;&gt;\u70b9\u51fb\u6211\u54e6\uff01&lt;\/a&gt;<\/p>\n<p>[\/code]<\/p>\n<p>\u70b9\u51fb\u8be5\u94fe\u63a5, \u663e\u793a\u5bf9\u8bdd\u6846\u540e\u8df3\u8f6c\u9875\u9762\u3002\u7531\u6b64\u53ef\u77e5,\u9664\u4e86\u6267\u884c\u76d1\u542c\u4e8b\u4ef6\u8fd8\u4f1a\u89e6\u53d1\u6d4f\u89c8\u5668\u9ed8\u8ba4\u52a8\u4f5c;\u6267\u884c\u76d1\u542c\u4e8b\u4ef6\u5728\u524d,\u89e6\u53d1\u6d4f\u89c8\u5668\u9ed8\u8ba4\u52a8\u4f5c\u5728\u540e\u3002<br \/>\n\u8fd9\u91cc\u6709\u4e2a\u7ecf\u5178\u793a\u4f8b, \u6211\u4eec\u5e0c\u671b\u70b9\u51fb\u94fe\u63a5\u5728\u65b0\u7a97\u53e3\u6253\u5f00\u9875\u9762,\u4f46\u4e0d\u5e0c\u671b\u5f53\u524d\u9875\u9762\u8df3\u8f6c\u3002\u8fd9\u4e2a\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528 preventDefault() \u963b\u6b62\u540e\u9762\u5c06\u8981\u6267\u884c\u7684\u6d4f\u89c8\u5668\u9ed8\u8ba4\u52a8\u4f5c\u3002<\/p>\n<p>[code lang=\"html\"]<br \/>\n&lt;a id=&quot;link&quot; href=&quot;http:\/\/w3c.org&quot;&gt;W3C \u9996\u9875\u94fe\u63a5&lt;\/a&gt;<br \/>\n&lt;script&gt;<br \/>\n    \/\/\u5728\u65b0\u7a97\u53e3, \u6253\u5f00\u9875\u9762<br \/>\n    document.getElementById('link').onclick = function(ev) {<br \/>\n        \/\/ \u963b\u6b62\u6d4f\u89c8\u5668\u9ed8\u8ba4\u52a8\u4f5c (\u9875\u9762\u8df3\u8f6c)<br \/>\n        ev.preventDefault();<br \/>\n        \/\/ \u5728\u65b0\u7a97\u53e3\u6253\u5f00\u9875\u9762<br \/>\n        window.open(this.href);<br \/>\n    };<br \/>\n&lt;\/script&gt;<br \/>\n[\/code]<\/p>\n<p><strong>return false<\/strong><\/p>\n<p>\u9000\u51fa\u6267\u884c,return false \u4e4b\u540e\u7684\u6240\u6709\u89e6\u53d1\u4e8b\u4ef6\u548c\u52a8\u4f5c\u90fd\u4e0d\u4f1a\u88ab\u6267\u884c\u3002\u6709\u65f6\u5019 return false \u53ef\u4ee5\u7528\u6765\u66ff\u4ee3 stopPropagation() \u548c preventDefault()\uff0c\u6bd4\u5982\u6211\u4eec\u4e0a\u9762\u65b0\u7a97\u53e3\u6253\u5f00\u94fe\u63a5\u7684\u4f8b\u5b50,\u5982:<\/p>\n<p>[code lang=\"html\"]<br \/>\n&lt;a id=&quot;link&quot; href=&quot;http:\/\/w3c.org&quot;&gt;W3C \u9996\u9875\u94fe\u63a5&lt;\/a&gt;<br \/>\n&lt;script&gt;<br \/>\n    \/\/\u5728\u65b0\u7a97\u53e3, \u6253\u5f00\u9875\u9762<br \/>\n    document.getElementById('link').onclick = function(ev) {<br \/>\n        \/\/ \u5728\u65b0\u7a97\u53e3\u6253\u5f00\u9875\u9762<br \/>\n        window.open(this.href);<br \/>\n        \/\/ \u9000\u51fa\u6267\u884c (\u5728\u76d1\u542c\u4e8b\u4ef6\u4e4b\u540e\u6267\u884c\u7684\u6d4f\u89c8\u5668\u9ed8\u8ba4\u52a8\u4f5c\u5c06\u4e0d\u4f1a\u88ab\u6267\u884c)<br \/>\n        return false;<br \/>\n    };<br \/>\n&lt;\/script&gt;<br \/>\n[\/code]<\/p>\n<p>\u6709\u4eba\u8ba4\u4e3a return false = stopPropagation() + preventDefault(),\u5176\u5b9e\u662f\u9519\u7684\u3002return false \u4e0d\u4f46\u963b\u6b62\u4e8b\u4ef6,\u8fd8\u53ef\u4ee5\u8fd4\u56de\u5bf9\u8c61,\u8df3\u51fa\u5faa\u73af\u7b49... \u65b9\u4fbf\u5730\u4e00\u5200\u5207\u800c\u4e0d\u591f\u7075\u6d3b,\u6ee5\u7528\u6613\u51fa\u9519.<\/p>\n<blockquote><p>\n\u201creturn false\u201d\u4e4b\u6240\u4ee5\u88ab\u8bef\u7528\u7684\u5982\u6b64\u5389\u5bb3\uff0c\u662f\u56e0\u4e3a\u5b83\u770b\u8d77\u6765\u50cf\u662f\u5b8c\u6210\u4e86\u6211\u4eec\u4ea4\u7ed9\u5b83\u7684\u5de5\u4f5c\uff0c\u6d4f\u89c8\u5668\u4e0d\u4f1a\u518d\u5c06\u6211\u4eec\u91cd\u5b9a\u5411\u5230 href \u4e2d\u7684\u94fe\u63a5\uff0c\u8868\u5355\u4e5f\u4e0d\u4f1a\u88ab\u7ee7\u7eed\u63d0\u4ea4\uff0c\u4f46\u8fd9\u4e48\u505a\u5230\u5e95\u6709\u4ec0\u4e48\u4e0d\u5bf9\u5462\uff1f\n<\/p><\/blockquote>\n<p>\u53ef\u80fd\u5728\u4f60\u521a\u5f00\u59cb\u5b66\u4e60\u5173\u4e8ejQuery\u4e8b\u4ef6\u5904\u7406\u65f6\uff0c\u770b\u5230\u7684\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u5c31\u662f\u5173\u4e8e\u5982\u4f55\u963b\u6b62\u6d4f\u89c8\u5668\u6267\u884c\u9ed8\u8ba4\u884c\u4e3a\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u6bb5\u6f14\u793aclick\u4e8b\u4ef6\u7684\u4ee3\u7801<\/p>\n<p>[code lang=\"javascript\"]<br \/>\n$(&quot;a.toggle&quot;).click(function () {<br \/>\n    $(&quot;#mydiv&quot;).toggle();<br \/>\n    return false; \/\/ Prevent browser from visiting `#`<br \/>\n}); <\/p>\n<p>[\/code]<\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u4f7f\u7528toggle\u6765\u663e\u793a\u6216\u8005\u9690\u85cf #mydiv\uff0c\u7136\u540e\u963b\u6b62\u6d4f\u89c8\u5668\u7ee7\u7eed\u8bbf\u95eehref\u4e2d\u6307\u5b9a\u7684\u94fe\u63a5\u3002<\/p>\n<p>\u50cf\u4e0a\u9762\u8fd9\u6837\u7684\u4f8b\u5b50\u4f1a\u8ba9\u7528\u6237\u517b\u6210\u4f7f\u7528\u201creturn false\u201d\u6765\u963b\u6b62\u6d4f\u89c8\u5668\u6267\u884c\u9ed8\u8ba4\u884c\u4e3a\u7684\u574f\u4e60\u60ef\uff0c\u5728\u8fd9\u7bc7\u6587\u7ae0\u91cc\uff0c\u6211\u5c06\u4f1a\u8ba8\u8bba\u5173\u4e8e\u963b\u6b62\u6d4f\u89c8\u5668\u6267\u884c\u9ed8\u8ba4\u884c\u4e3a\u7684\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u4e3b\u9898\uff1a<\/p>\n<blockquote><p>\n<strong>\u9009\u62e9\u6b63\u786e\u7684\u65b9\u6cd5\uff1a<\/strong>return false\u8fd8\u662fpreventDefault\uff0cstopPropagation\u6216\u8005stopImmediatePropagation<br \/>\n<strong>\u9009\u62e9\u5408\u9002\u7684\u4f4d\u7f6e\uff0c\u5f00\u59cb\uff0c\u7ed3\u675f\uff0c\u8fd8\u662f\u4e2d\u95f4\u67d0\u4e2a\u5730\u65b9\uff1a<\/strong>\u4f60\u5e94\u8be5\u5728\u4e8b\u4ef6\u56de\u8c03\u7684\u54ea\u4e2a\u90e8\u5206\u53d6\u6d88\u6d4f\u89c8\u5668\u6267\u884c\u9ed8\u8ba4\u884c\u4e3a\uff1f\n<\/p><\/blockquote>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u5f53\u6211\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u63d0\u5230event bubbling\uff08\u4e8b\u4ef6\u5192\u6ce1\uff09\uff0c\u6211\u60f3\u8868\u8fbe\u7684\u662f\u5927\u90e8\u5206\u4e8b\u4ef6\u90fd\u662f\u5148\u5728\u521d\u59cbDOM\u4e0a\u89e6\u53d1\uff0c\u7136\u540e\u518d\u901a\u8fc7DOM\u6811\u5f80\u4e0a\uff0c\u5728\u6bcf\u4e00\u7ea7\u7236\u5143\u7d20\u4e0a\u89e6\u53d1\uff0c\u4e8b\u4ef6\u4e0d\u4f1a\u5728\u5144\u5f1f\u8282\u70b9\u6216\u662f\u5b50\u8282\u70b9\u4e0a\u5192\u6ce1\uff08\u5f53\u4e8b\u4ef6\u5411\u4e0b\u5192\u6ce1\u65f6\uff0c\u6211\u4eec\u53eb\u5b83\u4e8b\u4ef6\u6355\u6349\uff08event capturing))\uff0c\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u4e8b\u4ef6\u8d77\u6ce1\u548c\u6355\u6349\u7684\u4ecb\u7ecd\u3002<\/p>\n<p><strong>\u9009\u62e9\u6b63\u786e\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u201creturn false\u201d\u4e4b\u6240\u4ee5\u88ab\u8bef\u7528\u7684\u5982\u6b64\u5389\u5bb3\uff0c\u662f\u56e0\u4e3a\u5b83\u770b\u8d77\u6765\u50cf\u662f\u5b8c\u6210\u4e86\u6211\u4eec\u4ea4\u7ed9\u5b83\u7684\u5de5\u4f5c\uff0c\u6d4f\u89c8\u5668\u4e0d\u4f1a\u518d\u5c06\u6211\u4eec\u91cd\u5b9a\u5411\u5230href\u4e2d\u7684\u94fe\u63a5\uff0c\u8868\u5355\u4e5f\u4e0d\u4f1a\u88ab\u7ee7\u7eed\u63d0\u4ea4\uff0c\u4f46\u8fd9\u4e48\u505a\u5230\u5e95\u6709\u4ec0\u4e48\u4e0d\u5bf9\u5462\uff1f<\/p>\n<p><strong>\u201dreturn false\u201c\u5230\u5e95\u505a\u4e86\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u5f53\u4f60\u6bcf\u6b21\u8c03\u7528\u201dreturn false\u201c\u7684\u65f6\u5019\uff0c\u5b83\u5b9e\u9645\u4e0a\u505a\u4e863\u4ef6\u4e8b\u60c5\uff1a<\/p>\n<ol>\n<li>event.preventDefault();<\/li>\n<li>event.stopPropagation();<\/li>\n<li>\u505c\u6b62\u56de\u8c03\u51fd\u6570\u6267\u884c\u5e76\u7acb\u5373\u8fd4\u56de\u3002<\/li>\n<\/ol>\n<p>\u201c\u7b49\u7b49\u201d\uff0c\u4f60\u53eb\u4e86\u8d77\u6765\uff01\u6211\u53ea\u662f\u60f3\u8ba9\u6d4f\u89c8\u5668\u505c\u6b62\u7ee7\u7eed\u6267\u884c\u9ed8\u8ba4\u884c\u4e3a\u800c\u5df2\uff0c\u6211\u4e0d\u9700\u8981\u5b83\u53bb\u505a\u53e6\u59162\u4ef6\u4e8b\u3002<\/p>\n<p>\u8fd93\u4ef6\u4e8b\u4e2d\u7528\u6765\u963b\u6b62\u6d4f\u89c8\u5668\u7ee7\u7eed\u6267\u884c\u9ed8\u8ba4\u884c\u4e3a\u7684\u53ea\u6709preventDefault\uff0c\u9664\u975e\u4f60\u60f3\u8981\u505c\u6b62\u4e8b\u4ef6\u5192\u6ce1\uff0c\u5426\u5219\u4f7f\u7528return false\u4f1a\u4e3a\u4f60\u7684\u4ee3\u7801\u57cb\u4e0b\u5f88\u5927\u7684\u9690\u60a3\uff0c\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u771f\u5b9e\u7684\u4f8b\u5b50\u6765\u770b\u770b\u8fd9\u6837\u7684\u8bef\u7528\u4f1a\u9020\u6210\u4ec0\u4e48\u540e\u679c\uff1a<\/p>\n<p>\u8fd9\u662f\u6211\u4eec\u7528\u6765\u6f14\u793a\u7684HTML\uff1a<\/p>\n<p>[code lang=\"html\"]<br \/>\n&lt;div class=&quot;post&quot;&gt;<br \/>\n&lt;h2&gt;&lt;a href=&quot;http:\/\/jb51.net&quot;&gt;My Page&lt;\/a&gt;&lt;\/h2&gt;<br \/>\n&lt;div class=&quot;content&quot;&gt;<br \/>\n    Teaser text...<br \/>\n  &lt;\/div&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;div class=&quot;post&quot;&gt;<br \/>\n&lt;h2&gt;&lt;a href=&quot;http:\/\/jb51.net&quot;&gt;My Other Page&lt;\/a&gt;&lt;\/h2&gt;<br \/>\n&lt;div class=&quot;content&quot;&gt;<br \/>\n    Teaser text...<br \/>\n  &lt;\/div&gt;<br \/>\n&lt;\/div&gt;<\/p>\n<p>[\/code]<\/p>\n<p>\u73b0\u5728\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u5728\u7528\u6237\u70b9\u51fb\u6587\u7ae0\u6807\u9898\u65f6\uff0c\u5c06\u6587\u7ae0\u52a8\u6001\u8f7d\u5165\u5230div.contentd\u4e2d\uff1a<\/p>\n<p>[code lang=\"html\"]<br \/>\njQuery(document).ready(function ($) {<br \/>\n  $(&quot;div.post h2 a&quot;).click(function () {<br \/>\n    var a    = $(this),<br \/>\n    href = a.attr('href'), \/\/ Let jQuery normalize `href`,<br \/>\n    content  = a.parent().next();<br \/>\n    content.load(href + &quot; #content&quot;);<br \/>\n    return false; \/\/ &quot;cancel&quot; the default behavior of following the link<br \/>\n  });<br \/>\n});<\/p>\n<p>[\/code]<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff08\u81f3\u5c11\u76ee\u524d\u662f\uff09\uff0c\u4f46\u5982\u679c\u6211\u4eec\u987a\u7740\u8fd9\u4e2a\u601d\u8def\u7ee7\u7eed\uff0c\u5982\u679c\u6211\u60f3\u8981\u5728\u7528\u6237\u70b9\u51fb\u4e86\u4e00\u4e2adiv.post\u5143\u7d20\uff08\u6216\u8005\u4efb\u4f55\u4e00\u4e2a\u5b83\u7684\u5b50\u5143\u7d20\uff09\u65f6\uff0c\u7ed9\u5b83\u52a0\u4e0a\u4e00\u4e2aactive\u7c7b\uff0c\u6211\u5c31\u9700\u8981\u7ed9div.post\u589e\u52a0\u4e86\u4e00\u4e2aclick\u56de\u8c03\uff1a<\/p>\n<p>[code lang=\"javascript\"]<br \/>\n\/\/ Inside Document Ready:<br \/>\nvar posts = $(&quot;div.post&quot;);<br \/>\n  posts.click(function () {<br \/>\n  \/\/ Remove active from all div.post<br \/>\n  posts.removeClass(&quot;active&quot;);<br \/>\n  \/\/ Add it back to this one<br \/>\n  $(this).addClass(&quot;active&quot;);<br \/>\n});<\/p>\n<p>[\/code]<\/p>\n<p>\u73b0\u5728\uff0c\u5982\u679c\u6211\u4eec\u70b9\u51fb\u4e00\u4e2a\u5e16\u5b50\u7684\u6807\u9898\uff0c\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5de5\u4f5c\u5417\uff1f\u7b54\u6848\u662f\u4e0d\u4f1a\uff0c\u56e0\u4e3a\u6211\u4eec\u5728\u6807\u9898\u7684click\u56de\u8c03\u91cc\u4f7f\u7528\u4e86return false\u800c\u4e0d\u662f\u6211\u4eec\u5e94\u8be5\u4f7f\u7528\u7684\uff0c\u201dreturn false\u201c\u7b49\u4e8eevent.preventDefault();\u52a0event.stopPropagation();\uff0c\u6240\u4ee5\u4e8b\u4ef6\u5192\u6ce1\u5c31\u88ab\u7ec8\u6b62\u4e86\uff0cclick\u4e8b\u4ef6\u4e0d\u4f1a\u88ab\u5192\u6ce1\u5230div.post\u4e0a\uff0c\u6211\u4eec\u4e3a\u5b83\u6dfb\u52a0\u7684\u4e8b\u4ef6\u56de\u8c03\u5f53\u7136\u4e5f\u5c31\u4e0d\u4f1a\u88ab\u8c03\u7528\u4e86\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u628a\u5b83\u548clive\u6216\u8005delegate\u4e8b\u4ef6\u6df7\u5728\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u60c5\u51b5\u5c31\u66f4\u7cdf\u4e86\u3002<\/p>\n<p>[code lang=\"javascript\"]<br \/>\n$(&quot;a&quot;).click(function () {<br \/>\n  \/\/ do something<br \/>\n  return false;<br \/>\n});  <\/p>\n<p>$(&quot;a&quot;).live(&quot;click&quot;, function () {<br \/>\n  \/\/ THIS WON'T FIRE<br \/>\n});<\/p>\n<p>[\/code]<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u771f\u6b63\u9700\u8981\u7684\u662f\u4ec0\u4e48\u5462\uff1f<\/p>\n<p><strong>preventDefault()<\/strong><\/p>\n<p>\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5f53\u4f60\u4f7f\u7528return false\u65f6\uff0c\u4f60\u5176\u5b9e\u771f\u6b63\u9700\u8981\u7684\u662fe.preventDefault()\u3002\u8981\u4f7f\u7528e.preventDefault\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u4f60\u4f20\u9012\u4e86event\u53c2\u6570\u5230\u4f60\u7684\u56de\u6389\u51fd\u6570\u4e2d\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\uff0c\u5c31\u662f\u90a3\u4e2ae\uff09\uff1a<\/p>\n<p>[code lang=\"javascript\"]<br \/>\n$(&quot;a&quot;).click(function (e) {<br \/>\n  \/\/ e == our event data<br \/>\n  e.preventDefault();<br \/>\n});<\/p>\n<p>[\/code]<\/p>\n<p>\u5b83\u4f1a\u66ff\u6211\u4eec\u5b8c\u6210\u6240\u6709\u5de5\u4f5c\uff0c\u4f46\u4e0d\u4f1a\u963b\u6b62\u7236\u8282\u70b9\u7ee7\u7eed\u5904\u7406\u4e8b\u4ef6\uff0c\u8981\u8bb0\u4f4f\uff0c\u4f60\u653e\u5728\u4ee3\u7801\u4e2d\u7684\u9650\u5236\u8d8a\u5c11\uff0c\u4f60\u7684\u4ee3\u7801\u5c31\u8d8a\u7075\u6d3b\uff0c\u4e5f\u5c31\u8d8a\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<p><strong>stopPropagation()<\/strong><\/p>\n<p>\u4f46\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u6709\u53ef\u80fd\u9700\u8981\u505c\u6b62\u4e8b\u4ef6\u5192\u6ce1\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n<p>[code lang=\"html\"]<br \/>\n&lt;div class=&quot;post&quot;&gt;<br \/>\n    Normal text and then a &lt;a href=&quot;http:\/\/jb51.net&quot;&gt;link&lt;\/a&gt; and then more text.<br \/>\n&lt;\/div&gt;<\/p>\n<p>[\/code]<\/p>\n<p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u5047\u8bbe\u5982\u679c\u4f60\u70b9\u4e86div\u4e0a\u9664\u4e86a\u94fe\u63a5\u4e4b\u5916\u7684\u5730\u65b9\uff0c\u6211\u4eec\u5e0c\u671b\u80fd\u53d1\u751f\u70b9\u4ec0\u4e48\u4e8b\u60c5\uff08\u6bd4\u5982\u6539\u53d8\u4e0b\u80cc\u666f\u4ec0\u4e48\u7684\uff09\uff0c\u4f46\u662f\u4e0d\u80fd\u5f71\u54cd\u7528\u6237\u70b9\u51fba\u94fe\u63a5\u7684\u884c\u4e3a\uff08\u4ece\u53ef\u7528\u6027\u7684\u89d2\u5ea6\uff0c\u8fd9\u4e2a\u4f8b\u5b50\u4e0d\u600e\u4e48\u597d\uff0c\u4f60\u53ef\u80fd\u4e0d\u5e0c\u671b\u7528\u6237\u70b9\u51fb\u522b\u7684\u5730\u65b9\u65f6\u53d1\u751f\u4efb\u4f55\u4e8b\u60c5\uff09\u3002<\/p>\n<p>[code lang=\"javascript\"]<br \/>\n$(&quot;div.post&quot;).click(function () {<br \/>\n  \/\/ Do the first thing;<br \/>\n});  <\/p>\n<p>$(&quot;div.post a&quot;).click(function (e) {<br \/>\n  \/\/ Don't cancel the browser's default action<br \/>\n  \/\/ and don't bubble this event!<br \/>\n  e.stopPropagation();<br \/>\n});<\/p>\n<p>[\/code]<\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528return false\uff0cdiv\u7684click\u4e8b\u4ef6\u4e0d\u4f1a\u88ab\u89e6\u53d1\uff0c\u4f46\u662f\u7528\u6237\u4e5f\u4e0d\u4f1a\u5230\u8fbe\u4ed6\u4eec\u70b9\u7684\u90a3\u4e2a\u94fe\u63a5\u3002<\/p>\n<p><strong>stopImmediatePropagation()<\/strong><\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u505c\u6b62\u4e00\u4e2a\u4e8b\u4ef6\u7ee7\u7eed\u6267\u884c\uff0c\u5373\u4f7f\u5f53\u524d\u7684\u5bf9\u8c61\u4e0a\u8fd8\u7ed1\u5b9a\u4e86\u5176\u5b83\u5904\u7406\u51fd\u6570\uff0c\u6240\u6709\u7ed1\u5b9a\u5728\u4e00\u4e2a\u5bf9\u8c61\u4e0a\u7684\u4e8b\u4ef6\u4f1a\u6309\u7ed1\u5b9a\u987a\u5e8f\u6267\u884c\uff0c\u770b\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n<p>[code lang=\"javascript\"]<br \/>\n$(&quot;div a&quot;).click(function () {<br \/>\n  \/\/ Do something<br \/>\n});  <\/p>\n<p>$(&quot;div a&quot;).click(function (e) {<br \/>\n  \/\/ Do something else<br \/>\n  e.stopImmediatePropagation();<br \/>\n});  <\/p>\n<p>$(&quot;div a&quot;).click(function () {<br \/>\n  \/\/ THIS NEVER FIRES<br \/>\n});  <\/p>\n<p>$(&quot;div&quot;).click(function () {<br \/>\n  \/\/ THIS NEVER FIRES<br \/>\n});<\/p>\n<p>[\/code]<\/p>\n<p>\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97\u8fd9\u4e2a\u4f8b\u5b50\u770b\u8d77\u6765\u5f88\u522b\u626d\uff0c\u6ca1\u9519\uff0c\u5c3d\u7ba1\u5982\u6b64\uff0c\u4f46\u6709\u65f6\u8fd9\u7684\u786e\u4f1a\u53d1\u751f\uff0c\u5982\u679c\u4f60\u7684\u4ee3\u7801\u975e\u5e38\u590d\u6742\uff0c\u90a3\u4e48\u4e0d\u540c\u7684widgets\u548cplugin\u5c31\u6709\u53ef\u80fd\u5728\u540c\u4e00\u4e2a\u5bf9\u8c61\u4e0a\u6dfb\u52a0\u4e8b\u4ef6\uff0c\u5982\u679c\u9047\u5230\u8fd9\u79cd\u60c5\u51b5\uff0c\u90a3\u4f60\u5c31\u5f88\u6709\u5fc5\u8981\u7406\u89e3\u548c\u4f7f\u7528stopImmediatePropagation\u3002<\/p>\n<p><strong>return false<\/strong><\/p>\n<p>\u53ea\u6709\u5f53\u4f60\u540c\u65f6\u9700\u8981preventDefault\u548cstopPropagation\uff0c\u5e76\u4e14\u4f60\u7684\u4ee3\u7801\u53ef\u4ee5\u63a5\u53d7\u76f4\u5230\u4f60\u7684\u56de\u8c03\u6267\u884c\u5b8c\u6210\u624d\u505c\u6b62\u6267\u884c\u6d4f\u89c8\u5668\u7684\u9ed8\u8ba4\u884c\u4e3a\uff0c\u90a3\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528\u201dreturn false\u201c\u3002\u4f46\u6211\u5f3a\u70c8\u5efa\u8bae\u4f60\u522b\u5728\u5199\u7ed9\u5176\u5b83jQuery\u5f00\u53d1\u8005\u7684\u6f14\u793a\u4ee3\u7801\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u9020\u6210\u66f4\u591a\u8bef\u7528\uff0c\u53ea\u6709\u5728\u4f60\u786e\u4fe1\u975e\u7528\u4e0d\u53ef\u7684\u60c5\u51b5\u4e0b\u518d\u53bb\u4f7f\u7528\u201dreturn false\u201c\u3002<\/p>\n<p>\u9009\u62e9\u9002\u5f53\u7684\u4f4d\u7f6e<\/p>\n<p>\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u201dreturn false\u201c\uff0c\u5b83\u53ea\u4f1a\u5728\u4f60\u7684\u56de\u8c03\u51fd\u6570\u6267\u884c\u7ed3\u675f\u624d\u53bb\u53d6\u6d88\u6d4f\u89c8\u5668\u7684\u9ed8\u8ba4\u884c\u4e3a\uff0c\u4f46\u662f\u4f7f\u7528e.preventDefault\uff0c\u6211\u4eec\u6709\u66f4\u591a\u7684\u9009\u62e9\uff0c\u5b83\u53ef\u4ee5\u968f\u65f6\u505c\u6b62\u6d4f\u89c8\u5668\u6267\u884c\u9ed8\u8ba4\u52a8\u4f5c\uff0c\u800c\u4e0d\u7ba1\u4f60\u5c06\u5b83\u653e\u5728\u51fd\u6570\u7684\u54ea\u4e2a\u90e8\u5206\u3002<\/p>\n<ol>\n<li>\u5f00\u53d1\u9636\u6bb5\uff0c\u4f60\u5e94\u8be5\u603b\u662f\u5c06\u5b83\u653e\u5728\u7b2c\u4e00\u884c\u3002\u4f60\u6700\u4e0d\u60f3\u505a\u7684\u4e8b\u60c5\u53ef\u80fd\u5c31\u662f\u4f60\u6b63\u5728\u8c03\u8bd5\u5c06\u4e00\u4e2aform\u6539\u6210ajax\u63d0\u4ea4\u7684\u65f6\u5019\uff0c\u5b83\u5374\u5df2\u7ecf\u88ab\u6309\u7167\u8001\u65b9\u6cd5\u63d0\u4ea4\u4e86\u3002<\/p>\n<\/li>\n<li>\n<p>\u4ea7\u54c1\u9636\u6bb5\uff0c\u5982\u679c\u4f60\u91c7\u7528\u4e86\u6e10\u8fdb\u589e\u5f3a\uff08progressive enhancement\uff09\uff0c\u90a3\u5c31\u628a\u5b83\u653e\u5230\u56de\u8c03\u7684\u7ed3\u675f\u4f4d\u7f6e\uff0c\u6216\u8005\u662f\u903b\u8f91\u7ec8\u70b9\uff0c\u5982\u679c\u5728\u4e00\u4e2a\u666e\u901a\u9875\u9762\u91c7\u7528\u6e10\u8fdb\u589e\u5f3a\uff0c\u90a3\u4f60\u5c31\u9700\u8981\u5728\u670d\u52a1\u5668\u7aef\u8003\u8651\u5982\u679c\u6d4f\u89c8\u5668\u4e0d\u652f\u6301JS\u65f6\uff08\u6216\u8005\u88ab\u7981\u7528\u65f6\uff09\uff0c\u5bf9\u94fe\u63a5\u7684click\u4e8b\u4ef6\u548c\u8868\u5355\u7684\u63d0\u4ea4\u4e8b\u4ef6\u7684\u5904\u7406\u3002\u8fd9\u91cc\u7684\u597d\u5904\u662f\uff0c\u6211\u4eec\u4e0d\u8003\u8651\u5173\u95edJS\u7684\u60c5\u51b5\uff0c\u53ea\u8003\u8651\u652f\u6301js\u65f6\u7684\u5f3a\u72c2\uff0c\u5982\u679c\u4f60\u7684\u56de\u8c03\u4ee3\u7801\u51fa\u9519\u629b\u51fa\u4e86\u5f02\u5e38\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<\/li>\n<\/ol>\n<p>[code lang=\"javascript\"]<br \/>\nvar data = {};<br \/>\n$(&quot;a&quot;).click(function (e) {<br \/>\n  e.preventDefault(); \/\/ cancel default behavior<br \/>\n  \/\/ Throws an error because `my` is undefined<br \/>\n  $(&quot;body&quot;).append(data.my.link);<br \/>\n  \/\/ The original link doesn't work AND the &quot;cool&quot;<br \/>\n  \/\/ JavaScript has broken. The user is left with NOTHING!<br \/>\n});<\/p>\n<p>[\/code]<\/p>\n<p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u540c\u6837\u7684\u4e8b\u4ef6\uff0c\u628apreventDefault\u8c03\u7528\u653e\u5728\u5e95\u90e8\u7684\u6548\u679c\uff1a<\/p>\n<p>[code lang=\"javascript\"]<br \/>\nvar data = {};<br \/>\n$(&quot;a&quot;).click(function (e) {<br \/>\n  \/\/ Throws an error because `my` is undefined<br \/>\n  $(&quot;body&quot;).append(data.my.link);  <\/p>\n<p>  \/\/ This line is never reached, and your website<br \/>\n  \/\/ falls back to using the `href` instead of this<br \/>\n  \/\/ &quot;cool&quot; broken JavaScript!<br \/>\n  e.preventDefault(); \/\/ cancel default behavior<br \/>\n});<br \/>\n[\/code]<\/p>\n<p>\u8fd9\u5bf9\u8868\u5355\u63d0\u4ea4\u4e5f\u540c\u6837\u6709\u6548\uff0c\u4f60\u53ef\u4ee5\u66f4\u597d\u7684\u5e94\u5bf9\u51fa\u9519\u7684\u60c5\u51b5\uff0c\u522b\u6307\u671b\u4f60\u7684\u4ee3\u7801\u4e00\u76f4\u6b63\u5e38\u5de5\u4f5c\uff0c\u5728\u53d1\u751f\u9519\u8bef\u65f6\u6709\u6b63\u786e\u7684\u5e94\u5bf9\u603b\u80dc\u8fc7\u5047\u8bbe\u4ee3\u7801\u4e0d\u4f1a\u51fa\u9519\u3002<\/p>\n<p>3.\u5728\u4ea7\u54c1\u9636\u6bb5\uff0c\u5982\u679c\u529f\u80fd\u8fd9\u8bbe\u8ba1JS\uff0c\u90a3\u5c31\u8fd8\u5e94\u8be5\u653e\u5728\u7b2c\u4e00\u884c\u3002<\/p>\n<p>\u8bb0\u4f4f\uff0c\u4e0d\u5fc5\u975e\u5f97\u662f\u51fd\u6570\u7684\u7b2c\u4e00\u884c\uff0c\u4f46\u662f\u8d8a\u65e9\u8d8a\u597d\uff0c\u8fd9\u91cc\u7684\u539f\u5219\u662f\uff1a\u5982\u679c\u51fd\u6570\u7684\u529f\u80fd\u662f\u901a\u8fc7JS\u5b9e\u73b0\u7684\uff08\u4e0d\u6d89\u53ca\u670d\u52a1\u7aef\u4ea4\u4e92\uff09\uff0c\u90a3\u5c31\u6ca1\u5fc5\u8981\u8003\u8651\u517c\u5bb9\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6dfb\u52a0\u5728\u7b2c\u4e00\u884c\u53ef\u4ee5\u9632\u6b62URL\u4e2d\u51fa\u73b0#\u5b57\u7b26\uff0c\u4f46\u663e\u7136\uff0c\u4f60\u8fd8\u662f\u5e94\u8be5\u5c3d\u53ef\u80fd\u591a\u7684\u589e\u52a0\u4e9b\u9519\u8bef\u5904\u7406\u4ee3\u7801\uff0c\u4ee5\u9632\u6b62\u7528\u6237\u5728\u51fa\u9519\u65f6\u53d8\u5f97\u4e0d\u77e5\u6240\u63aa\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9605\u8bfb\u672c\u6587\u4e4b\u524d\u4f60\u6700\u597d\u4e86\u89e3JavaScript\u5728\u6d4f\u89c8\u5668\u4e2d \u4e8b\u4ef6\u5192\u6ce1 \u548c \u4e8b\u4ef6\u6355\u83b7 \u4e24\u4e2a\u6982\u5ff5\u3002 \u56e0\u4e3aHTML\u7ed3\u6784\u4e2d\u5b58\u5728\u7740\u7236\u3001\u5b50&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,5],"tags":[],"class_list":["post-1140","post","type-post","status-publish","format-standard","hentry","category-fe","category-javascript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts\/1140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1140"}],"version-history":[{"count":12,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts\/1140\/revisions"}],"predecessor-version":[{"id":1764,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=\/wp\/v2\/posts\/1140\/revisions\/1764"}],"wp:attachment":[{"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feizhaojun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}