{"id":491,"date":"2019-06-13T11:30:31","date_gmt":"2019-06-13T03:30:31","guid":{"rendered":"https:\/\/wp.winter-weng.cc\/work\/?p=491"},"modified":"2019-06-13T11:30:31","modified_gmt":"2019-06-13T03:30:31","slug":"js-designmode-%e8%88%87-execcommand","status":"publish","type":"post","link":"https:\/\/wp.winter-weng.cc\/work\/?p=491","title":{"rendered":"[JS] designMode \u8207 execCommand"},"content":{"rendered":"\n<p>\u6700\u8fd1\u5c0f\u5925\u4f34\u5728\u7814\u7a76\u6846\u9078 Highlight \u7684\u529f\u80fd\uff0c\u4e00\u822c\u90fd\u6bd4\u8f03\u76f4\u89ba\u5730\u63a1\u7528 <code>window.getSelection()<\/code> \u548c regex\u3001\u53d6\u4ee3\u7684\u65b9\u5f0f\u4f86\u8655\u7406\uff0c\u4f46\u9019\u5728\u6587\u672c\u8907\u96dc\u7684\u60c5\u6cc1\u4e0b\uff0c\u4f8b\u5982\u6709\u591a\u500b tag \u5305\u88f9\u6642\uff0c\u5bb9\u6613\u6703\u6709\u554f\u984c\uff0c\u6216\u662f\u7a0b\u5f0f\u78bc\u96e3\u4ee5\u7dad\u8b77 (\u60f3\u50cf\u5404\u5f0f\u5404\u6a23\u7684\u6b63\u898f\u8868\u793a\u5f0f&#8230;)\u3002<\/p>\n\n\n<p>\u624d\u758f\u5b78\u6dfa\uff0c\u76f4\u81f3\u6b64\u523b\u624d\u8a8d\u8b58\u5230\u4e86 <code><a rel=\"noreferrer noopener\" aria-label=\"document.designMode (\u5728\u65b0\u5206\u9801\u4e2d\u958b\u555f)\" href=\"https:\/\/developer.mozilla.org\/zh-TW\/docs\/Web\/API\/Document\/designMode\" target=\"_blank\">document.designMode<\/a><\/code> \u548c <code><a rel=\"noreferrer noopener\" aria-label=\"document.execCommand() (\u5728\u65b0\u5206\u9801\u4e2d\u958b\u555f)\" href=\"https:\/\/developer.mozilla.org\/zh-TW\/docs\/Web\/API\/Document\/execCommand\" target=\"_blank\">document.execCommand()<\/a><\/code> \u65b9\u6cd5\uff0c\u5be6\u5728\u662f\u611f\u5230\u9a5a\u70ba\u5929\u4eba\uff5e\u56e0\u70ba\u4e5f\u6c92\u6709\u592a\u6df1\u5165\u7814\u7a76\uff0c\u5728\u6b64\u53ea\u505a\u500b\u7d00\u9304\u548c\u8cc7\u6e90\u53c3\u8003\u3002<\/p>\n\n\n<p>\u8981\u73a9\u770b\u770b\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7\u90e8\u843d\u683c\u6587\u7ae0\uff1a<a rel=\"noreferrer noopener\" aria-label=\"\uff08HTML\uff09\u7528 contenteditable \u8207 designMode \u5275\u9020\u81ea\u5df1\u7684 HTML \u7de8\u8f2f\u5668 (\u5728\u65b0\u5206\u9801\u4e2d\u958b\u555f)\" href=\"http:\/\/qupyqupy.pixnet.net\/blog\/post\/207305728-%E7%94%A8-contenteditable-%E8%88%87-designmode-%E5%89%B5%E9%80%A0%E8%87%AA%E5%B7%B1%E7%9A%84-html-%E7%B7%A8\" target=\"_blank\">\uff08HTML\uff09\u7528 contenteditable \u8207 designMode \u5275\u9020\u81ea\u5df1\u7684 HTML \u7de8\u8f2f\u5668<\/a> (\u4f5c\u8005\uff1a\u6cd5\u5947)<\/p>\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n<p>\u571f\u70ae\u7684 getSelection() Demo\uff1a<\/p>\n\n\n<p class=\"codepen\" data-height=\"265\" data-theme-id=\"0\" data-default-tab=\"js,result\" data-user=\"yichinweng\" data-slug-hash=\"xNMody\" style=\"height: 265px;align-items: center;justify-content: center;border: 2px solid;margin: 1em 0;padding: 1em\" data-pen-title=\"highlight selection\">\n  <span>See the Pen <a href=\"https:\/\/codepen.io\/yichinweng\/pen\/xNMody\/\">\n  highlight selection<\/a> by Winter (<a href=\"https:\/\/codepen.io\/yichinweng\">@yichinweng<\/a>)\n  on <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/span>\n<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n<p>\u4f7f\u7528 designMode \u7684\u7248\u672c (Fork <a rel=\"noreferrer noopener\" aria-label=\"Arturo Campos (\u5728\u65b0\u5206\u9801\u4e2d\u958b\u555f)\" href=\"https:\/\/codepen.io\/arturocr\/\" target=\"_blank\">Arturo Campos<\/a> )\uff1a<\/p>\n\n\n<p class=\"codepen\" data-height=\"265\" data-theme-id=\"0\" data-default-tab=\"html,result\" data-user=\"yichinweng\" data-slug-hash=\"QXwrVR\" style=\"height: 265px;align-items: center;justify-content: center;border: 2px solid;margin: 1em 0;padding: 1em\" data-pen-title=\"Highlight color with execCommand\">\n  <span>See the Pen <a href=\"https:\/\/codepen.io\/yichinweng\/pen\/QXwrVR\/\">\n  Highlight color with execCommand<\/a> by Winter (<a href=\"https:\/\/codepen.io\/yichinweng\">@yichinweng<\/a>)\n  on <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/span>\n<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5c0f\u5925\u4f34\u5728\u7814\u7a76\u6846\u9078 Highlight \u7684\u529f\u80fd\uff0c&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[4],"tags":[21],"class_list":["post-491","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript"],"_links":{"self":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=491"}],"version-history":[{"count":0,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/491\/revisions"}],"wp:attachment":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}