{"id":26361,"date":"2024-11-24T13:57:06","date_gmt":"2024-11-24T05:57:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26361\/"},"modified":"2024-11-24T13:57:06","modified_gmt":"2024-11-24T05:57:06","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-typescript-%e4%b8%ad%e7%ba%a6%e6%9d%9f%e5%af%b9%e8%b1%a1%e4%b8%ba-css-%e5%b1%9e%e6%80%a7%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26361\/","title":{"rendered":"\u5982\u4f55\u5728 TypeScript \u4e2d\u7ea6\u675f\u5bf9\u8c61\u4e3a CSS \u5c5e\u6027\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173089515159189.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 TypeScript \u4e2d\u7ea6\u675f\u5bf9\u8c61\u4e3a CSS \u5c5e\u6027\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 TypeScript \u4e2d\u7ea6\u675f\u5bf9\u8c61\u4e3a CSS \u5c5e\u6027\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728  \u4e2d\u7ea6\u675f\u5bf9\u8c61\u4e3a  \u5c5e\u6027\uff1f<\/strong><\/p>\n<p>\u5728 typescript \u4e2d\uff0c\u4e3a\u7279\u5b9a\u76ee\u7684\u800c\u7ea6\u675f\u5bf9\u8c61\u7c7b\u578b\u662f\u5f88\u91cd\u8981\u7684\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7a76\u5982\u4f55\u5c06\u5bf9\u8c61\u7ea6\u675f\u4e3a\u5305\u542b css \u5c5e\u6027\u3002<\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u51fd\u6570\uff1a<\/p>\n<pre>function setattrstoelement(el: htmlelement, attr: record&lt;string, any&gt;) {\n  for (const key in attr) {\n    el.setattribute(key, attr[key])\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a htmlelement \u548c\u4e00\u4e2a\u5177\u6709\u4efb\u610f\u503c\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u7ea6\u675f\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u4f7f\u5176\u5305\u542b css \u5c5e\u6027\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u8c03\u7528\u6b64\u51fd\u6570\u65f6\uff0cide \u53ef\u4ee5\u81ea\u52a8\u8865\u5168 css \u5c5e\u6027\u952e\u503c\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528 react \u6216 csstype<\/strong><\/p>\n<p>\u5728 react \u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 react \u63d0\u4f9b\u7684 cssproperties \u7c7b\u578b\uff1a<\/p>\n<pre>import { cssproperties } from \"react\";\n\nfunction setattrstoelement(el: htmlelement, attr: cssproperties) {\n    for (const key in attr) {\n        el.setattribute(key, attr[key]);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u4ec5\u4e3a\u5df2\u77e5\u7684 css \u5c5e\u6027\u63d0\u4f9b\u952e\u503c\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u4e0d\u5728 react \u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 csstype \u5e93\uff1a<\/p>\n<pre>import * as CSS from 'csstype';\n\nfunction setAttrsToElement(el: HTMLElement, attr: CSS.Properties) {\n    for (const key in attr) {\n        el.setAttribute(key, attr[key]);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u63d0\u4f9b\u4e0e cssproperties \u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u9002\u7528\u4e8e\u975e react \u9879\u76ee\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 TypeScript \u4e2d\u7ea6\u675f\u5bf9\u8c61\u4e3a CSS \u5c5e\u6027\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 \u4e2d\u7ea6\u675f\u5bf9\u8c61\u4e3a \u5c5e\u6027\uff1f \u5728 typescript \u4e2d\uff0c\u4e3a\u7279\u5b9a\u76ee\u7684\u800c\u7ea6\u675f\u5bf9\u8c61\u7c7b\u578b\u662f\u5f88\u91cd\u8981\u7684\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7a76\u5982\u4f55\u5c06\u5bf9\u8c61\u7ea6\u675f\u4e3a\u5305\u542b css \u5c5e\u6027\u3002 \u8003\u8651\u4ee5\u4e0b\u51fd\u6570\uff1a function setattrstoelement(el: htmlelement, attr: record&lt;string, any&gt;) { for (const key in attr) { el.setattribute(key, attr[key]) } } \u767b\u5f55\u540e\u590d\u5236 \u8be5\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a htmlelement \u548c\u4e00\u4e2a\u5177\u6709\u4efb\u610f\u503c\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u7ea6\u675f\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u4f7f\u5176\u5305\u542b css \u5c5e\u6027\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4f8b\u5982\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u8c03\u7528\u6b64\u51fd\u6570\u65f6\uff0cide \u53ef\u4ee5\u81ea\u52a8\u8865\u5168 css \u5c5e\u6027\u952e\u503c\u3002 \u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528 react \u6216 csstype \u5728 react \u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 react \u63d0\u4f9b\u7684 cssproperties \u7c7b\u578b\uff1a import { cssproperties } from &#8220;react&#8221;; function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-26361","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=26361"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26361\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}