{"id":3333,"date":"2024-11-10T12:02:58","date_gmt":"2024-11-10T04:02:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3333\/"},"modified":"2024-11-10T12:02:58","modified_gmt":"2024-11-10T04:02:58","slug":"js%e5%a6%82%e4%bd%95%e7%82%b9%e5%87%bb%e6%9f%90%e4%b8%aa%e6%8c%89%e9%92%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3333\/","title":{"rendered":"js\u5982\u4f55\u70b9\u51fb\u67d0\u4e2a\u6309\u94ae"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 click() \u65b9\u6cd5\u6a21\u62df\u7528\u6237\u70b9\u51fb\u6309\u94ae\u7684\u884c\u4e3a\uff0c\u6b65\u9aa4\u5982\u4e0b:\u83b7\u53d6\u6309\u94ae\u5143\u7d20\u3002\u8c03\u7528\u83b7\u53d6\u7684\u6309\u94ae\u5143\u7d20\u7684 click() \u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110809553041882.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u70b9\u51fb\u67d0\u4e2a\u6309\u94ae\u63d2\u56fe\" alt=\"js\u5982\u4f55\u70b9\u51fb\u67d0\u4e2a\u6309\u94ae\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u70b9\u51fb\u67d0\u4e2a\u6309\u94ae<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u5229\u7528 click() \u65b9\u6cd5\u6a21\u62df\u7528\u6237\u70b9\u51fb\u67d0\u4e2a\u6309\u94ae\u7684\u884c\u4e3a\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u6309\u94ae\u5143\u7d20\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 document.getElementById() \u6216 document.querySelector() \u6765\u83b7\u53d6\u8981\u70b9\u51fb\u7684\u6309\u94ae\u5143\u7d20\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u89e6\u53d1 click() \u65b9\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li>\u5bf9\u83b7\u53d6\u7684\u6309\u94ae\u5143\u7d20\u8c03\u7528 click() \u65b9\u6cd5\uff0c\u5373\u53ef\u6a21\u62df\u7528\u6237\u70b9\u51fb\u8be5\u6309\u94ae\u7684\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u83b7\u53d6\u6309\u94ae\u5143\u7d20\nconst button = document.getElementById(\"my-button\");\n\n\/\/ \u89e6\u53d1 click() \u65b9\u6cd5\nbutton.click();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u6309\u94ae\u5143\u7d20\u7981\u7528\u4e86\uff0cclick() \u65b9\u6cd5\u5c06\u4e0d\u8d77\u4f5c\u7528\u3002<\/li>\n<li>\u5982\u679c\u6309\u94ae\u5143\u7d20\u662f \uff0c\u8c03\u7528 click() \u65b9\u6cd5\u5c06\u63d0\u4ea4\u8868\u5355\u3002<\/li>\n<li>\u8fd8\u53ef\u4ee5\u4f7f\u7528 dispatchEvent() \u65b9\u6cd5\u89e6\u53d1 click \u4e8b\u4ef6\uff0c\u8fd9\u5bf9\u4e8e\u6d4b\u8bd5\u6216\u81ea\u52a8\u5316\u76ee\u7684\u5f88\u6709\u7528\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff08\u4f7f\u7528 dispatchEvent()\uff09\uff1a<\/strong><\/p>\n<pre>const button = document.getElementById(\"my-button\");\nconst event = new Event('click');\nbutton.dispatchEvent(event);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u70b9\u51fb\u67d0\u4e2a\u6309\u94ae\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>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 click() \u65b9\u6cd5\u6a21\u62df\u7528\u6237\u70b9\u51fb\u6309\u94ae\u7684\u884c\u4e3a\uff0c\u6b65\u9aa4\u5982\u4e0b:\u83b7\u53d6\u6309\u94ae\u5143\u7d20\u3002\u8c03\u7528\u83b7\u53d6\u7684\u6309\u94ae\u5143\u7d20\u7684 click() \u65b9\u6cd5\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u70b9\u51fb\u67d0\u4e2a\u6309\u94ae \u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u5229\u7528 click() \u65b9\u6cd5\u6a21\u62df\u7528\u6237\u70b9\u51fb\u67d0\u4e2a\u6309\u94ae\u7684\u884c\u4e3a\u3002 \u6b65\u9aa4\uff1a \u83b7\u53d6\u6309\u94ae\u5143\u7d20\uff1a \u4f7f\u7528 document.getElementById() \u6216 document.querySelector() \u6765\u83b7\u53d6\u8981\u70b9\u51fb\u7684\u6309\u94ae\u5143\u7d20\u3002 \u89e6\u53d1 click() \u65b9\u6cd5\uff1a \u5bf9\u83b7\u53d6\u7684\u6309\u94ae\u5143\u7d20\u8c03\u7528 click() \u65b9\u6cd5\uff0c\u5373\u53ef\u6a21\u62df\u7528\u6237\u70b9\u51fb\u8be5\u6309\u94ae\u7684\u884c\u4e3a\u3002 \u793a\u4f8b\uff1a \/\/ \u83b7\u53d6\u6309\u94ae\u5143\u7d20 const button = document.getElementById(&#8220;my-button&#8221;); \/\/ \u89e6\u53d1 click() \u65b9\u6cd5 button.click(); \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\u4e8b\u9879\uff1a \u5982\u679c\u6309\u94ae\u5143\u7d20\u7981\u7528\u4e86\uff0cclick() \u65b9\u6cd5\u5c06\u4e0d\u8d77\u4f5c\u7528\u3002 \u5982\u679c\u6309\u94ae\u5143\u7d20\u662f \uff0c\u8c03\u7528 click() \u65b9\u6cd5\u5c06\u63d0\u4ea4\u8868\u5355\u3002 \u8fd8\u53ef\u4ee5\u4f7f\u7528 dispatchEvent() \u65b9\u6cd5\u89e6\u53d1 click \u4e8b\u4ef6\uff0c\u8fd9\u5bf9\u4e8e\u6d4b\u8bd5\u6216\u81ea\u52a8\u5316\u76ee\u7684\u5f88\u6709\u7528\u3002 \u793a\u4f8b\uff08\u4f7f\u7528 dispatchEvent()\uff09\uff1a const button [&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-3333","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3333","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=3333"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3333\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}