{"id":312500,"date":"2024-05-20T22:40:28","date_gmt":"2024-05-20T14:40:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/312500.html"},"modified":"2024-05-20T22:40:35","modified_gmt":"2024-05-20T14:40:35","slug":"%e5%9c%a8%e7%bd%91%e9%a1%b5%e4%b8%8a%e9%9a%8f%e6%9c%ba%e6%98%be%e7%a4%ba10%e4%b8%aa%e4%b8%a4%e4%bd%8d%e6%95%b0%e7%9a%84js%e4%bb%a3%e7%a0%81%e6%80%8e%e4%b9%88%e5%86%99-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/312500.html","title":{"rendered":"\u5728\u7f51\u9875\u4e0a\u968f\u673a\u663e\u793a10\u4e2a\u4e24\u4f4d\u6570\u7684js\u4ee3\u7801\u600e\u4e48\u5199"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26233301\/0e235370-968e-49d6-b41f-1a312267dd19.webp\" alt=\"\u5728\u7f51\u9875\u4e0a\u968f\u673a\u663e\u793a10\u4e2a\u4e24\u4f4d\u6570\u7684js\u4ee3\u7801\u600e\u4e48\u5199\" \/><\/p>\n<p><p>\u968f\u673a\u5728\u7f51\u9875\u4e0a\u663e\u793a10\u4e2a\u4e24\u4f4d\u6570\uff0c\u53ef\u4ee5\u5229\u7528JavaScript\u7684<code>Math<\/code>\u5bf9\u8c61\u6765\u751f\u6210\u968f\u673a\u6570\uff0c\u5e76\u5229\u7528\u5faa\u73af\u6765\u8f93\u51fa\u8fd9\u4e9b\u6570\u3002<strong>\u751f\u6210\u968f\u673a\u4e24\u4f4d\u6570\u4f7f\u7528Math.floor(Math.random() * 90) + 10\u8868\u8fbe\u5f0f\u3001\u5c06\u7ed3\u679c\u6216\u6570\u7ec4\u8f93\u51fa\u5230\u7f51\u9875\u4f7f\u7528document.write\u6216\u64cd\u4f5cDOM\u3001\u5faa\u73af10\u6b21\u4ee5\u4ea7\u751f\u6240\u9700\u6570\u91cf\u7684\u6570\u5b57\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8be6\u7ec6\u63cf\u8ff0\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u73b0\u6b64\u529f\u80fd\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u5e0c\u671b\u5efa\u7acb\u4e00\u4e2a\u7b80\u5355\u7684HTML\u9875\u9762\uff0c\u5728\u9875\u9762\u4e0a\u4f7f\u7528JavaScript\u6765\u663e\u793a\u8fd9\u4e9b\u968f\u673a\u751f\u6210\u7684\u4e24\u4f4d\u6570\u3002\u4e0b\u9762\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u5c06\u6307\u5bfc\u4f60\u5b8c\u6210\u8fd9\u4e2a\u8fc7\u7a0b\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u7a7a\u6570\u7ec4\u6765\u5b58\u50a8\u751f\u6210\u7684\u968f\u673a\u6570\u3002\u7136\u540e\uff0c\u4f7f\u7528for\u5faa\u73af\u6765\u91cd\u590d\u751f\u6210\u968f\u673a\u6570\u7684\u8fc7\u7a0b10\u6b21\u3002\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\uff0c\u4f7f\u7528<code>Math.random()<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6570\uff0c\u4e58\u4ee590\u6765\u6269\u5c55\u5230\u6240\u9700\u7684\u8303\u56f4\uff08\u56e0\u4e3a\u4e24\u4f4d\u6570\u7684\u8303\u56f4\u662f10-99\uff0c\u537390\u4e2a\u53ef\u80fd\u7684\u503c\uff09\uff0c\u63a5\u7740\u4f7f\u7528<code>Math.floor()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u4e00\u4e2a\u6574\u6570\uff0c\u5e76\u5c0610\u52a0\u5230\u8fd9\u4e2a\u6570\u4e0a\uff0c\u786e\u4fdd\u7ed3\u679c\u662f\u4e00\u4e2a\u4e24\u4f4d\u6570\u3002\u6700\u7ec8\uff0c\u5c06\u5f97\u5230\u7684\u6570\u5b57\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\uff0c\u5e76\u901a\u8fc7\u9875\u9762\u4e0a\u7684\u4e00\u6bb5\u811a\u672c\u663e\u793a\u8fd9\u4e9b\u6570\u5b57\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001JS\u4ee3\u7801\u5b9e\u73b0<\/strong><\/h2>\n<p><pre><code class=\"language-javascript\">\/\/ \u521b\u5efa\u4e00\u4e2a\u7a7a\u6570\u7ec4\u6765\u5b58\u50a8\u968f\u673a\u6570<\/p>\n<p>var randomNumbers = [];<\/p>\n<p>\/\/ \u5faa\u73af10\u6b21\u751f\u6210\u968f\u673a\u6570<\/p>\n<p>for (var i = 0; i &lt; 10; i++) {<\/p>\n<p>    \/\/ \u751f\u6210\u4e00\u4e2a\u4e24\u4f4d\u6570\u7684\u968f\u673a\u6570<\/p>\n<p>    var randomNumber = Math.floor(Math.random() * 90) + 10;<\/p>\n<p>    \/\/ \u5c06\u968f\u673a\u6570\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d<\/p>\n<p>    randomNumbers.push(randomNumber);<\/p>\n<p>}<\/p>\n<p>\/\/ \u5c06\u968f\u673a\u6570\u6570\u7ec4\u8f93\u51fa\u5230\u7f51\u9875\u4e2d\uff0c\u53ef\u6839\u636e\u5b9e\u9645\u8981\u6c42\u8fdb\u884c\u6392\u7248<\/p>\n<p>document.write(&#039;&lt;h3&gt;Random Two-Digit Numbers:&lt;\/h3&gt;&#039;);<\/p>\n<p>document.write(&#039;&lt;ul&gt;&#039;);<\/p>\n<p>randomNumbers.forEach(function(number) {<\/p>\n<p>    document.write(&#039;&lt;li&gt;&#039; + number + &#039;&lt;\/li&gt;&#039;);<\/p>\n<p>});<\/p>\n<p>document.write(&#039;&lt;\/ul&gt;&#039;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001HTML\u6587\u6863\u7ed3\u6784<\/strong><\/h2>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;title&gt;Random Two-Digit Numbers Display&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        \/\/ JavaScript\u4ee3\u7801\u653e\u5728\u8fd9\u91cc<\/p>\n<p>    &lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06JavaScript\u4ee3\u7801\u653e\u5165HTML\u6587\u6863\u7684<code>&lt;script&gt;<\/code>\u6807\u7b7e\u5185\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u7f51\u9875\u52a0\u8f7d\u65f6\u6267\u884c\u8fd9\u6bb5\u811a\u672c\uff0c\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u663e\u793a\u751f\u6210\u768410\u4e2a\u968f\u673a\u4e24\u4f4d\u6570\u6570\u5b57\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p><strong>\u6ce8\u610f\uff1a\u786e\u4fdd\u4f7f\u7528<code>document.write<\/code>\u65b9\u6cd5\u65f6\u662f\u5728\u9875\u9762\u52a0\u8f7d\u7684\u521d\u671f\uff0c\u5982\u679c\u5728\u6587\u6863\u52a0\u8f7d\u5b8c\u6210\u540e\u4f7f\u7528\u53ef\u80fd\u4f1a\u8986\u76d6\u9875\u9762\u5185\u5bb9\u3002\u66f4\u597d\u7684\u529e\u6cd5\u662f\u64cd\u4f5cDOM\u6765\u52a8\u6001\u6dfb\u52a0\u5185\u5bb9\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u8bf7\u95ee\u5982\u4f55\u4f7f\u7528JavaScript\u7f16\u5199\u4e00\u4e2a\u4ea7\u751f\u968f\u673a\u4e24\u4f4d\u6570\u7684\u7f51\u9875\uff1f<\/strong><br \/>\n\u8981\u7f16\u5199\u4e00\u4e2a\u4ea7\u751f\u968f\u673a\u4e24\u4f4d\u6570\u7684\u7f51\u9875\uff0c\u53ef\u4ee5\u4f7f\u7528Math.random()\u51fd\u6570\u7ed3\u5408Math.floor()\u51fd\u6570\u6765\u751f\u6210\u4e00\u4e2a\u572810\u523099\u4e4b\u95f4\u7684\u968f\u673a\u6570\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u751f\u6210\u968f\u673a\u4e24\u4f4d\u6570\nvar randomNum = Math.floor(Math.random() * 90) + 10;\n\n\/\/ \u5728\u7f51\u9875\u4e2d\u663e\u793a\u968f\u673a\u6570\ndocument.write(&quot;\u968f\u673a\u751f\u6210\u7684\u4e24\u4f4d\u6570\u4e3a\uff1a&quot; + randomNum);\n<\/code><\/pre>\n<p><strong>2. \u5982\u4f55\u4f7f\u7f51\u9875\u4e0a\u663e\u793a\u7684\u968f\u673a\u4e24\u4f4d\u6570\u5b9e\u65f6\u66f4\u65b0\uff1f<\/strong><br \/>\n\u8981\u4f7f\u7f51\u9875\u4e0a\u663e\u793a\u7684\u968f\u673a\u4e24\u4f4d\u6570\u5b9e\u65f6\u66f4\u65b0\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b9a\u65f6\u5668setInterval()\u51fd\u6570\u6765\u5468\u671f\u6027\u5730\u751f\u6210\u65b0\u7684\u968f\u673a\u4e24\u4f4d\u6570\u3002\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u5b9a\u4e49\u751f\u6210\u968f\u673a\u4e24\u4f4d\u6570\u7684\u51fd\u6570\nfunction generateRandomNum() {\n  var randomNum = Math.floor(Math.random() * 90) + 10;\n  document.getElementById(&quot;random-number&quot;).innerHTML = &quot;\u968f\u673a\u751f\u6210\u7684\u4e24\u4f4d\u6570\u4e3a\uff1a&quot; + randomNum;\n}\n\n\/\/ \u6bcf\u96941\u79d2\u751f\u6210\u65b0\u7684\u968f\u673a\u4e24\u4f4d\u6570\nsetInterval(generateRandomNum, 1000);\n<\/code><\/pre>\n<p><strong>3. \u6211\u60f3\u5728\u7f51\u9875\u4e0a\u663e\u793a\u591a\u4e2a\u4e0d\u91cd\u590d\u7684\u968f\u673a\u4e24\u4f4d\u6570\uff0c\u5e94\u8be5\u5982\u4f55\u5b9e\u73b0\uff1f<\/strong><br \/>\n\u5982\u679c\u60f3\u8981\u5728\u7f51\u9875\u4e0a\u663e\u793a\u591a\u4e2a\u4e0d\u91cd\u590d\u7684\u968f\u673a\u4e24\u4f4d\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u5b58\u50a8\u5df2\u751f\u6210\u7684\u968f\u673a\u6570\uff0c\u6bcf\u6b21\u751f\u6210\u65b0\u7684\u968f\u673a\u6570\u65f6\uff0c\u5148\u68c0\u67e5\u6570\u7ec4\u4e2d\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u8be5\u6570\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u5c06\u5b83\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\u5e76\u663e\u793a\u5728\u7f51\u9875\u4e0a\u3002\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u5b9a\u4e49\u5b58\u50a8\u968f\u673a\u6570\u7684\u6570\u7ec4\nvar randomNumList = [];\n\n\/\/ \u5b9a\u4e49\u751f\u6210\u968f\u673a\u4e24\u4f4d\u6570\u4e14\u4e0d\u91cd\u590d\u7684\u51fd\u6570\nfunction generateUniqueRandomNum() {\n  var randomNum;\n  do {\n    randomNum = Math.floor(Math.random() * 90) + 10;\n  } while (randomNumList.includes(randomNum)); \/\/ \u68c0\u67e5\u6570\u7ec4\u4e2d\u662f\u5426\u5df2\u5b58\u5728\u8be5\u6570\n  randomNumList.push(randomNum); \/\/ \u5c06\u968f\u673a\u6570\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\n  document.write(&quot;\u968f\u673a\u751f\u6210\u7684\u4e24\u4f4d\u6570\u4e3a\uff1a&quot; + randomNum + &quot;&lt;br&gt;&quot;);\n}\n\n\/\/ \u751f\u62105\u4e2a\u4e0d\u91cd\u590d\u7684\u968f\u673a\u4e24\u4f4d\u6570\nfor (var i = 0; i &lt; 5; i++) {\n  generateUniqueRandomNum();\n}\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u751f\u62105\u4e2a\u4e0d\u91cd\u590d\u7684\u968f\u673a\u4e24\u4f4d\u6570\uff0c\u5e76\u5728\u7f51\u9875\u4e0a\u663e\u793a\u51fa\u6765\u3002\u5982\u679c\u9700\u8981\u751f\u6210\u66f4\u591a\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u8c03\u6574\u5faa\u73af\u6b21\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u968f\u673a\u5728\u7f51\u9875\u4e0a\u663e\u793a10\u4e2a\u4e24\u4f4d\u6570\uff0c\u53ef\u4ee5\u5229\u7528JavaScript\u7684Math\u5bf9\u8c61\u6765\u751f\u6210\u968f\u673a\u6570\uff0c\u5e76\u5229\u7528\u5faa\u73af\u6765\u8f93\u51fa\u8fd9\u4e9b\u6570\u3002 [&hellip;]","protected":false},"author":3,"featured_media":312508,"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\/312500"}],"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=312500"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/312500\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/312508"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=312500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=312500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=312500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}