{"id":11282,"date":"2024-07-18T23:31:51","date_gmt":"2024-07-18T15:31:51","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11282"},"modified":"2025-01-10T23:14:03","modified_gmt":"2025-01-10T15:14:03","slug":"css-custom-highlight-api","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2024\/07\/css-custom-highlight-api\/","title":{"rendered":"\u4eca\u65e5\u5b66\u4e60CSS\u6587\u672c\u81ea\u5b9a\u4e49\u9ad8\u4eaeAPI"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11282\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11282<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u72ec\u7acb\u57df\u540d\u4e2a\u4eba\u7f51\u7ad9\u65e0\u9700\u6388\u6743\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202407-css-highlight.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202407\/highlight-cover.png\" width=\"414\" height=\"261\" alt=\"\u6587\u672c\u9ad8\u4eae\u5c01\u9762\u5360\u4f4d\u56fe\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u524d\u524d\u524d\u8a00<\/h3>\n<p>    \u751f\u547d\u4e0d\u606f\uff0c\u5b66\u4e60\u4e0d\u6b62\u3002<\/p>\n<p>    \u672c\u5468\u5b9e\u64cd\u4e00\u4e0bCSS Custom Highlight API\u3002<\/p>\n<p>\u9996\u5148\uff0cFirefox\u6d4f\u89c8\u5668\u6700\u8fd1\u4e5f\u652f\u6301\u4e86\u4e0a\u8ff0API\uff0c\u81f3\u6b64\uff0c\u73b0\u4ee3\u6d4f\u89c8\u5668\u5b8c\u6210\u4e86\u5927\u7edf\u4e00\uff0c\u53ef\u4ee5\u5b66\u8d77\u6765\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202407\/2024-07-18_232418.png\" width=\"649\" height=\"255\" alt=\"Highlight API\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u8fd9\u4e2aAPI\u867d\u7136\u4ee5CSS\u5f00\u5934\uff0c\u4f46\u662f\u8981\u5b9e\u9645\u5e94\u7528\uff0c\u9700\u8981\u5bf9JavaScript\u6709\u4e00\u5b9a\u79ef\u7d2f\u7684\u5f00\u53d1\u4eba\u5458\u624d\u80fd\u9a7e\u9a6d\u3002<\/p>\n<p>    \u4e0d\u8fc7\u597d\u7684\u662f\u4ec0\u4e48\u5462\uff0c\u5176\u51e0\u4e2a\u7ecf\u5178\u7684\u5e94\u7528\u573a\u666f\uff0c\u4f8b\u5982\u641c\u7d22\u9ad8\u4eae\uff08MDN\u6587\u6863\u6709<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/CSS_Custom_Highlight_API\" target=\"_blank\" rel=\"nofollow noopener\">\u6848\u4f8b<\/a>\uff0c\u89c1\u4e0b\u56fe1\uff09\uff0c\u6216\u8005\u6e90\u4ee3\u7801\u9ad8\u4eae\uff08\u8fd9\u91cc\u6709<a href=\"https:\/\/code.juejin.cn\/pen\/7198487629262749756\" target=\"_blank\" rel=\"nofollow noopener\">\u6848\u4f8b<\/a>\uff0c\u89c1\u4e0b\u56fe2\uff09\uff0c\u76f8\u5173\u7684JS\u4ee3\u7801\u90fd\u662f\u56fa\u5b9a\u7684\uff0c\u5927\u5bb6\u76f4\u63a5\u590d\u5236\u7c98\u8d34\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002<\/p>\n<p>    <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202407\/2024-07-18_154328.png\" alt=\"MDN\u6848\u4f8b\u622a\u56fe\" width=\"569\" height=\"339\" \/><\/p>\n<p>    <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202407\/2024-07-18_154549.png\" alt=\"\u4ee3\u7801\u9ad8\u4eae\u6848\u4f8b\" width=\"707\" height=\"501\" \/><\/p>\n<p>    \u5386\u53f2\u7ecf\u9a8c\u544a\u8bc9\u6211\uff0c\u5916\u9762\u7684\u7ad9\u70b9\u90fd\u4e0d\u9760\u8c31\uff0c\u8fc7\u4e2a5\u5e7410\u5e74\u7684\uff0c\u8fd9\u4e9b\u5730\u5740\u90fd\u4e0d\u80fd\u8bbf\u95ee\u4e86\uff0c\u6240\u4ee5\uff0c\u6211\u505a\u4e86\u4e2a\u5907\u4efd\u5730\u5740\uff0c\u4ee5\u5907\u4e0d\u65f6\u4e4b\u9700\u3002<\/p>\n<p>\u5206\u522b\u53c2\u89c1\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/www.zhangxinxu.com\/study\/202407\/css-highlight-text-search-demo.php\" rel=\"noopener\" target=\"_blank\">CSS Highlight API\u6587\u672c\u9ad8\u4eae\u641c\u7d22demo<\/a><\/li>\n<li><a href=\"https:\/\/www.zhangxinxu.com\/study\/202407\/css-custom-highlight-code-demo.php\" rel=\"noopener\" target=\"_blank\">CSS Custom Highlight API\u5b9e\u73b0\u4ee3\u7801\u9ad8\u4eaedemo<\/a><\/li>\n<\/ul>\n<p>    OK\uff0c\u4e0b\u9762\u5f00\u59cb\u6b63\u5f0f\u4ecb\u7ecd\u5982\u4f55\u81eaDIY\u4e00\u4e2a\u6587\u672c\u9ad8\u4eae\u6548\u679c\u3002<\/p>\n<h3>\u4e8c\u3001\u6587\u672c\u9ad8\u4eae\u56db\u6b65\u9aa4<\/h3>\n<p>    \u5b9e\u73b0CSS\u7eaf\u6587\u672c\u9ad8\u4eae\u9700\u8981\u4e0b\u9762\u56db\u6b65\uff1a<\/p>\n<ol>\n<li>\u521b\u5efaRange\u5bf9\u8c61\uff08\u4f7f\u7528JS\uff09\uff1b<\/li>\n<li>\u4f7f\u7528\u7b2c\u4e00\u6b65\u521b\u5efa\u7684range\u521b\u5efaHighlight\u5bf9\u8c61\uff08\u4f7f\u7528JS\uff09\uff1b<\/li>\n<li>\u4f7f\u7528HighlightRegistry\u65b9\u6cd5\u6ce8\u518c\u4e0a\u4e00\u6b65\u521b\u5efa\u7684Highlight\u5bf9\u8c61\uff08\u4f7f\u7528JS\uff09\uff1b<\/li>\n<li>\u4f7f\u7528<code>::highlight()<\/code>\u4f2a\u5143\u7d20\u8ba9\u8bbe\u7f6e\u6ce8\u518c\u7684\u6587\u672c\u9009\u533a\u6837\u5f0f\uff08\u4f7f\u7528CSS\uff09\uff1b<\/li>\n<\/ol>\n<p>    \u7531\u6b64\u53ef\u89c1\uff0cCSS\u81ea\u5b9a\u4e49\u6587\u672c\u9ad8\u4eaeAPI\u5176\u5b9e\u662fJavaScript\u4ee3\u7801\u5b8c\u6210\u4e86\u5927\u90e8\u5206\u7684\u5de5\u4f5c\u3002<\/p>\n<p>    \u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u6848\u4f8b\u6f14\u793a\u5982\u4f55\u901a\u8fc7\u4e0a\u9762\u76844\u6b65\u64cd\u4f5c\uff0c\u5b9e\u73b0\u6587\u672c\u9ad8\u4eae\u6548\u679c\uff0c\u8ba9\u6211\u60f3\u4e2a\u4e0e\u4f17\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff0c\u55ef\u2026\u2026\u90a3\u5c31\u9875\u9762\u4e0a\u624b\u673a\u53f7\u7801\u4e0d\u660e\u6587\u663e\u793a\u6548\u679c\u5427\u3002<\/p>\n<p>    HTML\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;p id=\"p\"&gt;\u5982\u6709\u7591\u95ee\u53ef\u4ee5WX\u52a013208033621\u6216\u800515812345678\u54a8\u8be2\u3002&lt;\/p&gt;<\/pre>\n<p>    \u76ee\u6807\u5b9e\u73b0\u6548\u679c\u4e3a\uff1a<\/p>\n<p>    \u5219\u53ef\u4ee5\u5982\u4e0b4\u6b65\u5904\u7406\u3002<\/p>\n<h4>1. \u521b\u5efarange<\/h4>\n<p>\u4f7f\u7528<code>new Range()<\/code>\u8bed\u53e5\u6784\u5efa\uff0c\u7136\u540e\u8bbe\u7f6e<code>range<\/code>\u7684\u8d77\u6b62\u4f4d\u7f6e\u3002<\/p>\n<pre>\/\/ \u6587\u672c\u5b57\u7b26\u5185\u5bb9\r\nconst str = p.textContent;\r\n\/\/ \u624b\u673a\u53f7\u8d77\u59cb\u7d22\u5f15\u503c\r\nconst arrIndex = str.match(\/1\\d{10}\/g).map(tel => str.indexOf(tel));\r\n\/\/ \u521b\u5efarange\u6570\u7ec4\r\nconst arrRange = arrIndex.map(start => {\r\n  const range = new Range();\r\n  range.setStart(p.firstChild, start + 3);\r\n  range.setEnd(p.firstChild, start + 7);\r\n  \r\n  return range;\r\n});\r\n<\/pre>\n<h4>2. \u521b\u5efaHighlight\u5bf9\u8c61<\/h4>\n<p><code>new Highlight()<\/code>\u65b9\u6cd5\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>const highlight = new Highlight(range1, range2, ...);<\/pre>\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u4e48\u5904\u7406\uff1a<\/p>\n<pre>\/\/ \u5b9a\u4e49\u9ad8\u4eae\r\nconst highlight = new Highlight(...arrRange.flat());<\/pre>\n<h4>3. \u6ce8\u518c\u9ad8\u4eae<\/h4>\n<p>\u8fd9\u4e2a\u7b80\u5355\uff0c\u4e5f\u662f\u4e00\u884c\u4ee3\u7801\u7684\u4e8b\u60c5\uff1a<\/p>\n<pre>\/\/ \u6ce8\u518c\u9ad8\u4eae\r\nCSS.highlights.set(\"tel-highlight\", highlight);<\/pre>\n<h4>4. CSS\u5e94\u7528\u9ad8\u4eae<\/h4>\n<p>\u8f6e\u5230\u4f2a\u5143\u7d20<code>::highlight()<\/code>\u51fa\u9a6c\u4e86\uff0c\u53c2\u6570\u5c31\u662f\u4e0a\u9762\u7b2c3\u6b65\u6ce8\u518c\u7684\u9ad8\u4eae\u540d\u79f0\uff1a<\/p>\n<pre>::highlight(tel-highlight) {\r\n  background-color: currentColor;\r\n}<\/pre>\n<p>\u6b64\u65f6\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u5982\u56fe\u6240\u793a\u7684\u5c55\u793a\u6548\u679c\u4e86\uff0c\u624b\u673a\u53f7\u4e2d\u95f44\u4e2a\u6570\u5b57\u53d8\u6210\u4e86\u8272\u5757\uff0c\u4e0d\u53ef\u89c1\u4e86\u3002<\/p>\n<p>    <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202407\/2024-07-18_172726.png\" alt=\"\u8272\u5757\u663e\u793a\u793a\u610f\" width=\"437\" height=\"40\" style=\"border:1px solid #ddd;\" \/><\/p>\n<p>    \u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202407\/css-highlight-hide-tel-number-demo.php\" rel=\"noopener\" target=\"_blank\">CSS HighLight API\u9690\u85cf\u90e8\u5206\u624b\u673a\u53f7\u7801demo<\/a><\/p>\n<h3>\u4e09\u3001\u4e00\u4e9b\u8865\u5145\u8bf4\u660e\u548c\u7ed3\u8bed<\/h3>\n<p>    \u8865\u5145\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684range\u521b\u5efa\u8981\u6bd4\u4e0a\u9762\u7684\u793a\u4f8b\u590d\u6742\uff0c\u56e0\u4e3a\u9ad8\u4eae\u7684\u5143\u7d20\u4e2d\u4f1a\u6709N\u591a\u5143\u7d20\u548c\u8282\u70b9\u6df7\u6742\u5728\u4e00\u8d77\uff0c\u9700\u8981\u4f7f\u7528\u66f4\u52a0\u590d\u6742\u7684\u904d\u5386\u7b97\u6cd5\uff0c\u5177\u4f53\u53ef\u4ee5\u53c2\u8003\u672c\u6587\u4e00\u5f00\u59cb\u63d0\u4f9b\u7684\u4e24\u4e2a\u793a\u4f8b\u3002<\/li>\n<li><code>::highlight()<\/code>\u4f2a\u5143\u7d20\u662f\u4e0d\u652f\u6301CSS\u53d8\u91cf\u7684\uff0c\u4f8b\u5982\uff1a\n<pre>::highlight(highlight) {\r\n  --color: red;\r\n  color: var(--color);\r\n}<\/pre>\n<p>        \u662f\u4e0d\u4f1a\u8ba9\u6587\u5b57\u989c\u8272\u9ad8\u4eae\u4e3a\u7ea2\u8272\u7684\u3002<\/li>\n<li><code>::highlight()<\/code>\u4f2a\u5143\u7d20\u53ea\u652f\u6301\u90e8\u5206CSS\u5c5e\u6027\uff0c\u5305\u62ec\uff1a\n<ul>\n<li>\u6587\u672c\u989c\u8272 <code>color<\/code><\/li>\n<li>\u80cc\u666f\u989c\u8272 <code>background-color<\/code><\/li>\n<li>\u6587\u672c\u88c5\u9970\u7ebf <code>text-decoration<\/code><\/li>\n<li>\u6587\u672c\u9634\u5f71 <code>text-shadow<\/code><\/li>\n<li>\u6587\u672c\u63cf\u8fb9 <code>-webkit-text-stroke<\/code><\/li>\n<li>\u6587\u672c\u586b\u5145 <code>-webkit-text-fill-color<\/code><\/li>\n<\/ul>\n<p>        \u4e5f\u5c31\u662f\u6211\u4eec\u65e0\u6cd5\u8ba9\u9ad8\u4eae\u7684\u6587\u672c\u5b57\u53f7\u53d1\u751f\u53d8\u5316\uff08<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2022\/03\/css-size-adjust-font-unicode-range\/\">\u53ef\u4ee5\u4f7f\u7528size-adjust<\/a>\uff09\uff0c\u4f46\u662f\u53ef\u4ee5\u7ed9\u6587\u5b57\u52a0\u9634\u5f71\u3001\u6ce2\u6d6a\u7ebf\u3001\u63cf\u8fb9\u7b49\u3002\n        <\/li>\n<li>\u9ad8\u4eae\u7684\u6587\u672c\u662f\u65e0\u6cd5\u54cd\u5e94\u4e8b\u4ef6\u7684\uff0c\u53ea\u80fd\u7528\u6765\u7eaf\u6837\u5f0f\u53d8\u5316\u5904\u7406\u3002\u5982\u679c\u6709\u54cd\u5e94\u884c\u4e3a\u7684\u9700\u6c42\uff0c\u8fd8\u662f\u4f7f\u7528\u4f20\u7edf\u7684\u6807\u7b7e\u5305\u88f9\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<p>    \u597d\u5427\uff0c\u5c31\u8bf4\u8fd9\u4e48\u591a\uff0c\u7136\u540e\u53d1\u4e2a\u901a\u77e5\uff0c\u5468\u65e5\u6296\u97f3\u6211\u4f1a\u6709\u4e00\u573a\u8fde\u7ebf\u76f4\u64ad\uff08\u524d\u7aef\u76fc\u54e5\uff09\uff0c\u6211\u81ea\u5df1\u7684\u53f7\u521a\u5efa\u4e0d\u4e45\uff0c\u53eb\u201c\u5f20\u946b\u65ed\u672c\u4eba\u201d\uff0c\u6b22\u8fce\u5173\u6ce8\u54c8\uff0c\u5230\u65f6\u5019\u4f1a\u4ecb\u7ecd\u65b0\u4e66\uff0c\u4ee5\u53ca\u5c31\u524d\u7aef\u76f8\u5173\u95ee\u9898\u8fdb\u884c\u4e00\u4e9b\u4ea4\u6d41\uff0c\u8fd9\u662f\u6211\u9996\u6b21\u6296\u97f3\u76f4\u64ad\u554a\uff0c\u51b2\u51b2\u51b2\uff01<\/p>\n<p>    \u672c\u5468\u788e\u788e\u5ff5\u5c31\u4e0d\u5ff5\u4e86\uff0c\u514d\u5f97\u6df9\u4e86\u4e0a\u9762\u7684\u901a\u77e5\u3002<\/p>\n<p>    <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202407\/2024-7-18_174715.jpeg\" width=\"138\" height=\"138\" alt=\"\u51b2\u51b2\u51b2\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11282\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11282<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202407\/highlight-cover.png\" width=\"160\" height=\"101\" alt=\"\u6587\u672c\u9ad8\u4eae\u5c01\u9762\u5360\u4f4d\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u968f\u7740Firefox 131\u4e5f\u652f\u6301\u4e86CSS Custom Highlight API\uff0c\u81f3\u6b64\uff0c\u6240\u6709\u73b0\u4ee3\u6d4f\u89c8\u5668\u4e0b\u90fd\u53ef\u4ee5\u5b9e\u73b0\u65e0\u6807\u7b7e\u7684\u6587\u672c\u5185\u5bb9\u9ad8\u4eae\u6548\u679c\u4e86\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,1284],"tags":[1838,1729,215,1726,1987,587,996,430,383],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11282"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=11282"}],"version-history":[{"count":1,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11282\/revisions"}],"predecessor-version":[{"id":11543,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11282\/revisions\/11543"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}