{"id":2069,"date":"2025-02-26T23:29:15","date_gmt":"2025-02-26T15:29:15","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2069"},"modified":"2025-02-26T23:29:15","modified_gmt":"2025-02-26T15:29:15","slug":"javascript-%e9%aa%8c%e8%af%81-api","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/26\/javascript-%e9%aa%8c%e8%af%81-api\/","title":{"rendered":"JavaScript \u9a8c\u8bc1 API"},"content":{"rendered":"\n<p>JavaScript \u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u7f6e\u7684 API\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5b9e\u73b0\u66f4\u5f3a\u5927\u7684\u8868\u5355\u9a8c\u8bc1\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684 JavaScript \u9a8c\u8bc1 API\uff0c\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u662f\u5426\u7b26\u5408\u7279\u5b9a\u8981\u6c42\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. <code>HTMLFormElement.checkValidity()<\/code><\/strong><\/h3>\n\n\n\n<p><code>checkValidity()<\/code> \u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u8868\u5355\u4e2d\u6240\u6709\u8f93\u5165\u5b57\u6bb5\u7684\u6709\u6548\u6027\uff0c\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u8868\u5355\u662f\u5426\u6709\u6548\u3002\u5b83\u4f1a\u68c0\u67e5\u6bcf\u4e2a\u8868\u5355\u5143\u7d20\u7684 <code>required<\/code> \u5c5e\u6027\u3001<code>pattern<\/code> \u5c5e\u6027\u4ee5\u53ca\u5176\u4ed6 HTML5 \u9a8c\u8bc1\u7279\u6027\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form id=\"myForm\"&gt;\n  &lt;label for=\"email\"&gt;Email:&lt;\/label&gt;\n  &lt;input type=\"email\" id=\"email\" name=\"email\" required \/&gt;\n\n  &lt;label for=\"age\"&gt;Age:&lt;\/label&gt;\n  &lt;input type=\"number\" id=\"age\" name=\"age\" required \/&gt;\n\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  var form = document.getElementById(\"myForm\");\n\n  form.onsubmit = function(event) {\n    event.preventDefault();\n\n    if (!form.checkValidity()) {\n      alert(\"Please fill out the form correctly.\");\n    } else {\n      alert(\"Form is valid!\");\n    }\n  };\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>checkValidity()<\/code> \u4f1a\u68c0\u67e5\u8868\u5355\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bb5\u662f\u5426\u7b26\u5408\u5176\u7ea6\u5b9a\u7684\u89c4\u5219\uff08\u4f8b\u5982\uff0c<code>required<\/code> \u5c5e\u6027\uff09\u3002\u5982\u679c\u8868\u5355\u65e0\u6548\uff0c\u9875\u9762\u4f1a\u63d0\u793a\u7528\u6237\u586b\u5199\u6b63\u786e\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. <code>HTMLInputElement.setCustomValidity()<\/code><\/strong><\/h3>\n\n\n\n<p><code>setCustomValidity()<\/code> \u65b9\u6cd5\u5141\u8bb8\u4f60\u4e3a\u8f93\u5165\u5143\u7d20\u8bbe\u7f6e\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u9a8c\u8bc1\u6d88\u606f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u8bbe\u7f6e\u4e00\u4e9b\u989d\u5916\u7684\u9a8c\u8bc1\u89c4\u5219\uff0c\u5e76\u63d0\u4f9b\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form id=\"myForm\"&gt;\n  &lt;label for=\"password\"&gt;Password:&lt;\/label&gt;\n  &lt;input type=\"password\" id=\"password\" name=\"password\" required \/&gt;\n\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  var passwordInput = document.getElementById(\"password\");\n\n  passwordInput.oninput = function() {\n    if (passwordInput.value.length &lt; 6) {\n      passwordInput.setCustomValidity(\"Password must be at least 6 characters long.\");\n    } else {\n      passwordInput.setCustomValidity(\"\");\n    }\n  };\n\n  document.getElementById(\"myForm\").onsubmit = function(event) {\n    event.preventDefault();\n\n    if (passwordInput.checkValidity()) {\n      alert(\"Form is valid!\");\n    } else {\n      alert(\"Password is invalid!\");\n    }\n  };\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u5bc6\u7801\u957f\u5ea6\u5c0f\u4e8e 6 \u4e2a\u5b57\u7b26\uff0c<code>setCustomValidity()<\/code> \u4f1a\u8bbe\u7f6e\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u6d88\u606f\uff0c\u63d0\u793a\u7528\u6237\u8f93\u5165\u66f4\u957f\u7684\u5bc6\u7801\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. <code>HTMLInputElement.reportValidity()<\/code><\/strong><\/h3>\n\n\n\n<p><code>reportValidity()<\/code> \u65b9\u6cd5\u4f1a\u89e6\u53d1\u6d4f\u89c8\u5668\u7684\u9ed8\u8ba4\u9a8c\u8bc1\u884c\u4e3a\uff0c\u5e76\u663e\u793a\u9519\u8bef\u6d88\u606f\u3002\u5b83\u4e0e <code>checkValidity()<\/code> \u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46 <code>reportValidity()<\/code> \u4f1a\u81ea\u52a8\u5c55\u793a\u9a8c\u8bc1\u5931\u8d25\u7684\u6d88\u606f\uff0c\u800c <code>checkValidity()<\/code> \u4ec5\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form id=\"myForm\"&gt;\n  &lt;label for=\"email\"&gt;Email:&lt;\/label&gt;\n  &lt;input type=\"email\" id=\"email\" name=\"email\" required \/&gt;\n\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  var form = document.getElementById(\"myForm\");\n\n  form.onsubmit = function(event) {\n    event.preventDefault();\n\n    if (!form.reportValidity()) {\n      alert(\"Form contains errors.\");\n    } else {\n      alert(\"Form submitted successfully.\");\n    }\n  };\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>reportValidity()<\/code> \u65b9\u6cd5\u4f1a\u81ea\u52a8\u68c0\u67e5\u8868\u5355\u5e76\u663e\u793a\u6240\u6709\u9a8c\u8bc1\u9519\u8bef\uff0c\u7528\u6237\u53ef\u4ee5\u770b\u5230\u8be6\u7ec6\u7684\u9519\u8bef\u63d0\u793a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. <code>Pattern<\/code> \u5c5e\u6027<\/strong><\/h3>\n\n\n\n<p>HTML5 \u63d0\u4f9b\u4e86 <code>pattern<\/code> \u5c5e\u6027\uff0c\u5b83\u5141\u8bb8\u4f60\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u4e3a\u8868\u5355\u8f93\u5165\u5b57\u6bb5\u8bbe\u7f6e\u9a8c\u8bc1\u89c4\u5219\u3002\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u5c06\u4f1a\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5339\u914d\u3002\u5982\u679c\u5339\u914d\u5931\u8d25\uff0c\u8868\u5355\u5c06\u65e0\u6cd5\u63d0\u4ea4\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form id=\"myForm\"&gt;\n  &lt;label for=\"phone\"&gt;Phone:&lt;\/label&gt;\n  &lt;input type=\"text\" id=\"phone\" name=\"phone\" pattern=\"\\d{10}\" required \/&gt;\n\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  var form = document.getElementById(\"myForm\");\n\n  form.onsubmit = function(event) {\n    event.preventDefault();\n\n    if (!form.checkValidity()) {\n      alert(\"Please enter a valid phone number (10 digits).\");\n    } else {\n      alert(\"Form is valid!\");\n    }\n  };\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>pattern=\"\\d{10}\"<\/code> \u6307\u5b9a\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a 10 \u4f4d\u7684\u6570\u5b57\u4f5c\u4e3a\u7535\u8bdd\u53f7\u7801\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. <code>Constraint Validation API<\/code><\/strong><\/h3>\n\n\n\n<p>HTML5 \u7684\u8868\u5355\u9a8c\u8bc1\u4e5f\u5305\u542b\u4e86\u4e00\u4e2a <strong>\u7ea6\u675f\u9a8c\u8bc1 API<\/strong>\uff0c\u5b83\u901a\u8fc7\u8868\u5355\u5143\u7d20\u7684 <code>validity<\/code> \u5c5e\u6027\u6765\u62a5\u544a\u6bcf\u4e2a\u5b57\u6bb5\u7684\u9a8c\u8bc1\u72b6\u6001\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u5c5e\u6027\u6765\u68c0\u67e5\u5b57\u6bb5\u7684\u6709\u6548\u6027\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form id=\"myForm\"&gt;\n  &lt;label for=\"email\"&gt;Email:&lt;\/label&gt;\n  &lt;input type=\"email\" id=\"email\" name=\"email\" required \/&gt;\n\n  &lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\n  var emailInput = document.getElementById(\"email\");\n\n  emailInput.onblur = function() {\n    if (!emailInput.validity.valid) {\n      alert(\"Please enter a valid email address.\");\n    }\n  };\n\n  document.getElementById(\"myForm\").onsubmit = function(event) {\n    event.preventDefault();\n\n    if (emailInput.validity.valid) {\n      alert(\"Form is valid!\");\n    } else {\n      alert(\"Please fix the errors in the form.\");\n    }\n  };\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>validity<\/code> \u5c5e\u6027\u6765\u68c0\u67e5 <code>email<\/code> \u8f93\u5165\u5b57\u6bb5\u7684\u6709\u6548\u6027\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u4e86\u65e0\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u4f1a\u663e\u793a\u63d0\u793a\u6d88\u606f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p>JavaScript \u9a8c\u8bc1 API \u4e3a\u524d\u7aef\u5f00\u53d1\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8868\u5355\u9a8c\u8bc1\u5de5\u5177\u3002\u5e38\u89c1\u7684\u9a8c\u8bc1\u65b9\u6cd5\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>checkValidity()<\/code> \u548c <code>reportValidity()<\/code> \u6765\u68c0\u67e5\u8868\u5355\u7684\u6709\u6548\u6027\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>setCustomValidity()<\/code> \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u6d88\u606f\u3002<\/li>\n\n\n\n<li>\u5229\u7528 HTML5 \u7684 <code>pattern<\/code> \u5c5e\u6027\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u9a8c\u8bc1\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>validity<\/code> \u5c5e\u6027\u83b7\u53d6\u5b57\u6bb5\u7684\u9a8c\u8bc1\u72b6\u6001\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u7b26\u5408\u9884\u671f\u89c4\u5219\uff0c\u4ece\u800c\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u5e76\u51cf\u5c11\u9519\u8bef\u6570\u636e\u7684\u63d0\u4ea4\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u7f6e\u7684 API\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5b9e\u73b0\u66f4\u5f3a\u5927\u7684\u8868\u5355\u9a8c\u8bc1\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684 Ja [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2069","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2069"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2069\/revisions"}],"predecessor-version":[{"id":2070,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2069\/revisions\/2070"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}