{"id":1183,"date":"2024-11-10T12:25:36","date_gmt":"2024-11-10T04:25:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1183\/"},"modified":"2024-11-10T12:25:36","modified_gmt":"2024-11-10T04:25:36","slug":"%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3-cannot-call-method-addeventlistener-of-null-error-%e9%94%99%e8%af%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1183\/","title":{"rendered":"\u5982\u4f55\u89e3\u51b3 &#8220;Cannot call method &#8216;addEventListener&#8217; of null error&#8221; \u9519\u8bef\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173028653185298.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u89e3\u51b3 &#8220;Cannot call method &#8216;addEventListener&#8217; of null error&#8221; \u9519\u8bef\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u89e3\u51b3 &#8220;Cannot call method &#8216;addEventListener&#8217; of null error&#8221; \u9519\u8bef\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u89e3\u51b3 &#8220;cannot call method &#8216;addeventlistener&#8217; of null error&#8221; \u9519\u8bef<\/strong><\/p>\n<p>\u5728\u5f00\u53d1\u4e2d\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230 &#8220;cannot call method &#8216;addeventlistener&#8217; of null error&#8221; \u9519\u8bef\u3002\u6b64\u9519\u8bef\u901a\u5e38\u5728\u5c1d\u8bd5\u5728\u65e0\u6548\u6216\u4e0d\u5b58\u5728\u7684 dom \u5143\u7d20\u4e0a\u6dfb\u52a0\u4e8b\u4ef6\u4fa6\u542c\u5668\u65f6\u53d1\u751f\u3002<\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u60a8\u9700\u8981<strong>\u68c0\u67e5\u60a8\u5c1d\u8bd5\u7ed1\u5b9a\u4e8b\u4ef6\u4fa6\u542c\u5668\u5230\u7684\u5143\u7d20\u662f\u5426\u5b58\u5728<\/strong>\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\uff1a<\/p>\n<pre>if (element) {\n  \/\/ \u5143\u7d20\u5b58\u5728\uff0c\u53ef\u4ee5\u7ed1\u5b9a\u4e8b\u4ef6\u4fa6\u542c\u5668\n} else {\n  \/\/ \u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u4e0d\u80fd\u7ed1\u5b9a\u4e8b\u4ef6\u4fa6\u542c\u5668\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u8bf4\u660e\u4ee3\u7801\u8bd5\u56fe\u5728\u9519\u8bef\u7684\u5730\u65b9\u7ed1\u5b9a\u4e8b\u4ef6\u4fa6\u542c\u5668\u3002\u60a8\u9700\u8981\u627e\u5230\u6b63\u786e\u7684\u5143\u7d20\u5e76\u5c06\u5176\u7528\u4f5c &#8220;element&#8221;\u3002<\/p>\n<p><strong>\u4f8b\u5982\uff1a<\/strong><\/p>\n<pre>\/\/ \u5047\u8bbe button \u662f\u4e00\u4e2a DOM \u5143\u7d20\nif (button) {\n  button.addEventListener('click', function() {\n    \/\/ \u5904\u7406\u70b9\u51fb\u4e8b\u4ef6\n  });\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u662f\u5426\u5b58\u5728\u6309\u94ae\u5143\u7d20\uff0c\u7136\u540e\u624d\u4e3a\u5176\u7ed1\u5b9a\u4e00\u4e2a\u4fa6\u542c\u5668\u3002\u8fd9\u6709\u52a9\u4e8e\u907f\u514d &#8220;cannot call method &#8216;addeventlistener&#8217; of null error&#8221; \u9519\u8bef\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u89e3\u51b3 &#8220;Cannot call method &#8216;addEventListener&#8217; of null error&#8221; \u9519\u8bef\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u51b3 &#8220;cannot call method &#8216;addeventlistener&#8217; of null error&#8221; \u9519\u8bef \u5728\u5f00\u53d1\u4e2d\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230 &#8220;cannot call method &#8216;addeventlistener&#8217; of null error&#8221; \u9519\u8bef\u3002\u6b64\u9519\u8bef\u901a\u5e38\u5728\u5c1d\u8bd5\u5728\u65e0\u6548\u6216\u4e0d\u5b58\u5728\u7684 dom \u5143\u7d20\u4e0a\u6dfb\u52a0\u4e8b\u4ef6\u4fa6\u542c\u5668\u65f6\u53d1\u751f\u3002 \u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u60a8\u9700\u8981\u68c0\u67e5\u60a8\u5c1d\u8bd5\u7ed1\u5b9a\u4e8b\u4ef6\u4fa6\u542c\u5668\u5230\u7684\u5143\u7d20\u662f\u5426\u5b58\u5728\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\uff1a if (element) { \/\/ \u5143\u7d20\u5b58\u5728\uff0c\u53ef\u4ee5\u7ed1\u5b9a\u4e8b\u4ef6\u4fa6\u542c\u5668 } else { \/\/ \u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u4e0d\u80fd\u7ed1\u5b9a\u4e8b\u4ef6\u4fa6\u542c\u5668 } \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u8bf4\u660e\u4ee3\u7801\u8bd5\u56fe\u5728\u9519\u8bef\u7684\u5730\u65b9\u7ed1\u5b9a\u4e8b\u4ef6\u4fa6\u542c\u5668\u3002\u60a8\u9700\u8981\u627e\u5230\u6b63\u786e\u7684\u5143\u7d20\u5e76\u5c06\u5176\u7528\u4f5c &#8220;element&#8221;\u3002 \u4f8b\u5982\uff1a \/\/ \u5047\u8bbe button \u662f\u4e00\u4e2a DOM \u5143\u7d20 if (button) { button.addEventListener(&#8216;click&#8217;, function() { \/\/ \u5904\u7406\u70b9\u51fb\u4e8b\u4ef6 }); } \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u662f\u5426\u5b58\u5728\u6309\u94ae\u5143\u7d20\uff0c\u7136\u540e\u624d\u4e3a\u5176\u7ed1\u5b9a\u4e00\u4e2a\u4fa6\u542c\u5668\u3002\u8fd9\u6709\u52a9\u4e8e\u907f\u514d &#8220;cannot [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1183","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=1183"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1183\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}