{"id":168996,"date":"2024-05-08T16:36:12","date_gmt":"2024-05-08T08:36:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/168996.html"},"modified":"2024-05-08T16:36:19","modified_gmt":"2024-05-08T08:36:19","slug":"javascript-%e6%96%87%e6%9c%ac%e7%bc%96%e8%be%91%e6%a1%86-%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/168996.html","title":{"rendered":"javascript \u6587\u672c\u7f16\u8f91\u6846 \u600e\u4e48\u5b9e\u73b0\u7684"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27035258\/87313381-29e5-4528-9bfd-1c0b7e6dd4e1.webp\" alt=\"javascript \u6587\u672c\u7f16\u8f91\u6846 \u600e\u4e48\u5b9e\u73b0\u7684\" \/><\/p>\n<p><p>\u5b9e\u73b0JavaScript\u6587\u672c\u7f16\u8f91\u6846\u901a\u5e38\u9700\u8981\u7ed3\u5408HTML\u548cCSS\uff0c\u4ee5\u53caJavaScript\u7684\u5f3a\u5927\u529f\u80fd\u6765\u5b8c\u6210\u3002<strong>\u4e3b\u8981\u6d89\u53ca\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u6587\u672c\u57df\u3001\u5b9a\u5236\u6837\u5f0f\u3001\u76d1\u542c\u6587\u672c\u8f93\u5165\u4e8b\u4ef6\u3001\u5b9e\u73b0\u6587\u672c\u683c\u5f0f\u5316\u529f\u80fd\u3001\u4ee5\u53ca\u4fdd\u5b58\u548c\u6062\u590d\u7f16\u8f91\u5185\u5bb9<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u5b9e\u73b0\u6587\u672c\u683c\u5f0f\u5316\u529f\u80fd<\/strong>\u662f\u5173\u952e\u70b9\uff0c\u5b83\u80fd\u8ba9\u7528\u6237\u8f7b\u677e\u5730\u6dfb\u52a0\u683c\u5f0f\uff0c\u5982\u52a0\u7c97\u3001\u659c\u4f53\u6216\u4e0b\u5212\u7ebf\u7b49\uff0c\u4ece\u800c\u589e\u5f3a\u6587\u672c\u7684\u8868\u73b0\u529b\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5b9e\u73b0\u4e00\u4e2a\u5177\u6709\u57fa\u672c\u529f\u80fd\u7684JavaScript\u6587\u672c\u7f16\u8f91\u6846\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u57fa\u672c\u7684\u6587\u672c\u57df<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u6587\u672c\u7f16\u8f91\u6846\uff0c\u9996\u5148\u9700\u8981\u5728HTML\u4e2d\u521b\u5efa\u4e00\u4e2a\u6587\u672c\u57df\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>&lt;textarea&gt;<\/code>\u6807\u7b7e\u6765\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u672c\u8f93\u5165\u533a\u57df\u3002\u7136\u800c\uff0c\u82e5\u60f3\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6392\u7248\u548c\u683c\u5f0f\u5316\u9009\u9879\uff0c<code>&lt;div&gt;<\/code>\u6807\u7b7e\u914d\u5408<code>contenteditable<\/code>\u5c5e\u6027\u5219\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;div contenteditable=&quot;true&quot; id=&quot;textEditor&quot;&gt;&lt;\/div&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6837\u5f0f\u5b9a\u5236<\/h4>\n<\/p>\n<p><p>\u63a5\u7740\uff0c\u4f7f\u7528CSS\u6765\u4e3a\u6587\u672c\u7f16\u8f91\u6846\u5b9a\u5236\u6837\u5f0f\uff0c\u5305\u62ec\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u3001\u8fb9\u6846\u6837\u5f0f\u7b49\uff0c\u4f7f\u5176\u9002\u5e94\u7f51\u9875\u7684\u6574\u4f53\u8bbe\u8ba1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">#textEditor {<\/p>\n<p>  border: 1px solid #ccc;<\/p>\n<p>  min-height: 100px;<\/p>\n<p>  padding: 10px;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u76d1\u542c\u6587\u672c\u8f93\u5165\u4e8b\u4ef6<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u54cd\u5e94\u7528\u6237\u7684\u8f93\u5165\uff0c\u9700\u8981\u4f7f\u7528JavaScript\u6765\u76d1\u542c\u6587\u672c\u57df\u4e2d\u7684\u8f93\u5165\u4e8b\u4ef6\u3002\u901a\u8fc7\u76d1\u542c<code>input<\/code>\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u5b9e\u65f6\u83b7\u53d6\u5e76\u5904\u7406\u7528\u6237\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.getElementById(&#039;textEditor&#039;).addEventListener(&#039;input&#039;, function(event) {<\/p>\n<p>  console.log(event.target.innerHTML);<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u529f\u80fd\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5141\u8bb8\u4f60\u5728\u7528\u6237\u8f93\u5165\u6587\u672c\u65f6\u5373\u65f6\u6355\u6349\u5185\u5bb9\uff0c\u53ef\u7528\u4e8e\u5b9e\u65f6\u9884\u89c8\u3001\u4fdd\u5b58\u8349\u7a3f\u6216\u5176\u4ed6\u9700\u8981\u5373\u65f6\u54cd\u5e94\u6587\u672c\u53d8\u5316\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b9e\u73b0\u6587\u672c\u683c\u5f0f\u5316\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u8981\u5728\u6587\u672c\u7f16\u8f91\u6846\u5185\u5b9e\u73b0\u683c\u5f0f\u5316\u64cd\u4f5c\uff0c\u5982\u52a0\u7c97\u3001\u659c\u4f53\u7b49\uff0c\u53ef\u4ee5\u5229\u7528<code>document.execCommand<\/code>\u65b9\u6cd5\u3002\u5c3d\u7ba1\u8fd9\u4e2a\u65b9\u6cd5\u5df2\u7ecf\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u5e76\u53ef\u80fd\u5728\u672a\u6765\u88ab\u65b0\u7684API\u66ff\u4ee3\uff0c\u4f46\u5b83\u4ecd\u7136\u662f\u76ee\u524d\u5b9e\u73b0\u7b80\u5355\u6587\u672c\u7f16\u8f91\u529f\u80fd\u7684\u5feb\u6377\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u6309\u94ae<\/h4>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;button onclick=&quot;formatText(&#039;bold&#039;)&quot;&gt;\u52a0\u7c97&lt;\/button&gt;<\/p>\n<p>&lt;button onclick=&quot;formatText(&#039;italic&#039;)&quot;&gt;\u659c\u4f53&lt;\/button&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b9e\u73b0\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">function formatText(command) {<\/p>\n<p>  document.execCommand(command, false, null);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u70b9\u51fb\u6309\u94ae\uff0c\u6b64\u51fd\u6570\u53ef\u4ee5\u5c06\u7528\u6237\u9009\u5b9a\u7684\u6587\u672c\u8fdb\u884c\u52a0\u7c97\u6216\u8bbe\u7f6e\u4e3a\u659c\u4f53\uff0c\u4ece\u800c\u4e30\u5bcc\u6587\u672c\u7684\u8868\u73b0\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4fdd\u5b58\u548c\u6062\u590d\u7f16\u8f91\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5b9e\u73b0\u6587\u672c\u7f16\u8f91\u6846\u7684\u53e6\u4e00\u4e2a\u5173\u952e\u529f\u80fd\u662f\u4fdd\u5b58\u548c\u6062\u590d\u7f16\u8f91\u5185\u5bb9\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u672c\u5730\u5b58\u50a8\uff08\u5982localStorage\uff09\u6765\u5b9e\u73b0\uff0c\u4f7f\u7528\u6237\u5373\u4fbf\u5728\u5173\u95ed\u6d4f\u89c8\u5668\u540e\u4e5f\u80fd\u6062\u590d\u4e4b\u524d\u7684\u7f16\u8f91\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h4>\u4fdd\u5b58\u5185\u5bb9<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">function saveContent() {<\/p>\n<p>  const content = document.getElementById(&#039;textEditor&#039;).innerHTML;<\/p>\n<p>  localStorage.setItem(&#039;content&#039;, content);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6062\u590d\u5185\u5bb9<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">window.onload = function() {<\/p>\n<p>  const savedContent = localStorage.getItem(&#039;content&#039;);<\/p>\n<p>  if(savedContent) {<\/p>\n<p>    document.getElementById(&#039;textEditor&#039;).innerHTML = savedContent;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4fdd\u5b58\u548c\u52a0\u8f7d\u51fd\u6570\uff0c\u7528\u6237\u7684\u7f16\u8f91\u5185\u5bb9\u53ef\u4ee5\u88ab\u5b89\u5168\u5730\u5b58\u50a8\u5e76\u5728\u9700\u8981\u65f6\u6062\u590d\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bed<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u7684JavaScript\u6587\u672c\u7f16\u8f91\u6846\uff0c\u5305\u542b\u6587\u672c\u683c\u5f0f\u5316\u3001\u5185\u5bb9\u4fdd\u5b58\u548c\u6062\u590d\u7b49\u529f\u80fd\u3002\u5c3d\u7ba1<code>document.execCommand<\/code>\u7684\u4f7f\u7528\u5df2\u7ecf\u4e0d\u88ab\u63a8\u8350\uff0c\u8fd9\u63d0\u4f9b\u4e86\u5b9e\u73b0\u6587\u672c\u7f16\u8f91\u5668\u7684\u4e00\u4e2a\u8d77\u70b9\u3002\u968f\u7740Web\u6280\u672f\u7684\u53d1\u5c55\uff0c\u66f4\u4e3a\u5f3a\u5927\u548c\u5b89\u5168\u7684\u65b9\u6cd5\u5c06\u4f1a\u51fa\u73b0\uff0c\u4f46\u8fd9\u4e9b\u57fa\u672c\u6982\u5ff5\u5c06\u6301\u7eed\u9002\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\u98981\uff1a\u5982\u4f55\u901a\u8fc7JavaScript\u5b9e\u73b0\u4e00\u4e2a\u6587\u672c\u7f16\u8f91\u6846\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u8981\u901a\u8fc7JavaScript\u5b9e\u73b0\u4e00\u4e2a\u6587\u672c\u7f16\u8f91\u6846\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528HTML\u4e2d\u7684<textarea>\u6807\u7b7e\u6765\u521b\u5efa\u4e00\u4e2a\u6587\u672c\u8f93\u5165\u6846\uff0c\u7136\u540e\u7528JavaScript\u4ee3\u7801\u6765\u64cd\u4f5c\u8be5\u8f93\u5165\u6846\u3002\u53ef\u4ee5\u901a\u8fc7\u83b7\u53d6\u8f93\u5165\u6846\u7684\u5f15\u7528\u5e76\u4f7f\u7528JavaScript\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u6765\u6539\u53d8\u6587\u672c\u7f16\u8f91\u6846\u7684\u5916\u89c2\u548c\u529f\u80fd\uff0c\u6bd4\u5982\u6539\u53d8\u6587\u672c\u7f16\u8f91\u6846\u7684\u5927\u5c0f\u3001\u5b57\u4f53\u6837\u5f0f\u3001\u9650\u5236\u8f93\u5165\u5185\u5bb9\u7684\u957f\u5ea6\u7b49\u3002\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u6765\u54cd\u5e94\u7528\u6237\u5728\u6587\u672c\u7f16\u8f91\u6846\u4e2d\u8f93\u5165\u7684\u5185\u5bb9\uff0c\u8fdb\u884c\u5b9e\u65f6\u7684\u9a8c\u8bc1\u548c\u5904\u7406\u3002\u6240\u4ee5\u4f7f\u7528JavaScript\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6587\u672c\u7f16\u8f91\u6846\u3002<\/p>\n<p><strong>\u95ee\u98982\uff1a\u5982\u4f55\u4f7f\u7528JavaScript\u5bf9\u6587\u672c\u7f16\u8f91\u6846\u7684\u8f93\u5165\u5185\u5bb9\u8fdb\u884c\u9650\u5236\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u5728JavaScript\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5bf9\u6587\u672c\u7f16\u8f91\u6846\u7684\u8f93\u5165\u5185\u5bb9\u8fdb\u884c\u9650\u5236\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u7ed9\u6587\u672c\u7f16\u8f91\u6846\u7684&quot;oninput&quot;\u4e8b\u4ef6\u7ed1\u5b9a\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u5728\u8be5\u51fd\u6570\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5224\u65ad\u8f93\u5165\u7684\u5185\u5bb9\u662f\u5426\u7b26\u5408\u7279\u5b9a\u7684\u8981\u6c42\uff0c\u5982\u53ea\u5141\u8bb8\u8f93\u5165\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u7279\u5b9a\u5b57\u7b26\uff0c\u6216\u9650\u5236\u8f93\u5165\u7684\u6700\u5927\u957f\u5ea6\u7b49\u3002\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528JavaScript\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5982inputElement.value\u3001inputElement.maxLength\u7b49\u6765\u5b9e\u73b0\u5bf9\u6587\u672c\u7f16\u8f91\u6846\u7684\u8f93\u5165\u5185\u5bb9\u8fdb\u884c\u9650\u5236\u548c\u63a7\u5236\u3002<\/p>\n<p><strong>\u95ee\u98983\uff1a\u5982\u4f55\u901a\u8fc7JavaScript\u5b9e\u73b0\u6587\u672c\u7f16\u8f91\u6846\u7684\u5b57\u6570\u7edf\u8ba1\u529f\u80fd\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u8981\u5b9e\u73b0\u6587\u672c\u7f16\u8f91\u6846\u7684\u5b57\u6570\u7edf\u8ba1\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528JavaScript\u6765\u8ba1\u7b97\u6587\u672c\u7f16\u8f91\u6846\u7684\u8f93\u5165\u5b57\u7b26\u6570\u91cf\u3002\u5177\u4f53\u505a\u6cd5\u662f\uff0c\u901a\u8fc7\u83b7\u53d6\u6587\u672c\u7f16\u8f91\u6846\u7684\u5f15\u7528\u5e76\u76d1\u542c\u5176&quot;oninput&quot;\u4e8b\u4ef6\uff0c\u5f53\u7528\u6237\u5728\u6587\u672c\u7f16\u8f91\u6846\u4e2d\u8f93\u5165\u3001\u5220\u9664\u6216\u7c98\u8d34\u5b57\u7b26\u65f6\uff0c\u89e6\u53d1&quot;oninput&quot;\u4e8b\u4ef6\uff0c\u5e76\u5728\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\u83b7\u53d6\u6587\u672c\u7f16\u8f91\u6846\u7684inputElement.value\u5c5e\u6027\u503c\uff08\u8f93\u5165\u7684\u5185\u5bb9\uff09\uff0c\u7136\u540e\u4f7f\u7528JavaScript\u7684length\u5c5e\u6027\u83b7\u53d6\u8f93\u5165\u5185\u5bb9\u7684\u5b57\u7b26\u6570\u91cf\u3002\u63a5\u7740\uff0c\u4f60\u53ef\u4ee5\u5c06\u5b57\u7b26\u6570\u91cf\u5c55\u793a\u5728\u9875\u9762\u7684\u67d0\u4e2a\u5143\u7d20\u6216\u63d0\u793a\u6846\u5185\uff0c\u5b9e\u65f6\u663e\u793a\u7528\u6237\u8f93\u5165\u7684\u5b57\u6570\u3002\u8fd9\u6837\uff0c\u901a\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684JavaScript\u4ee3\u7801\uff0c\u5c31\u80fd\u5b9e\u73b0\u6587\u672c\u7f16\u8f91\u6846\u7684\u5b57\u6570\u7edf\u8ba1\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b9e\u73b0JavaScript\u6587\u672c\u7f16\u8f91\u6846\u901a\u5e38\u9700\u8981\u7ed3\u5408HTML\u548cCSS\uff0c\u4ee5\u53caJavaScript\u7684\u5f3a\u5927\u529f\u80fd\u6765\u5b8c\u6210\u3002\u4e3b\u8981 [&hellip;]","protected":false},"author":3,"featured_media":169005,"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\/168996"}],"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=168996"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/168996\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/169005"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=168996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=168996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=168996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}