{"id":26173,"date":"2024-11-24T11:29:34","date_gmt":"2024-11-24T03:29:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26173\/"},"modified":"2024-11-24T11:29:34","modified_gmt":"2024-11-24T03:29:34","slug":"%e5%a6%82%e4%bd%95%e7%ba%a6%e6%9d%9f-ts-%e5%87%bd%e6%95%b0%e5%8f%82%e6%95%b0%e4%b8%ba-css-%e5%b1%9e%e6%80%a7%e4%bb%a5%e8%8e%b7%e5%be%97%e4%bb%a3%e7%a0%81%e6%8f%90%e7%a4%ba%e5%bb%ba%e8%ae%ae%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26173\/","title":{"rendered":"\u5982\u4f55\u7ea6\u675f TS \u51fd\u6570\u53c2\u6570\u4e3a CSS \u5c5e\u6027\u4ee5\u83b7\u5f97\u4ee3\u7801\u63d0\u793a\u5efa\u8bae\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173085322179185.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7ea6\u675f TS \u51fd\u6570\u53c2\u6570\u4e3a CSS \u5c5e\u6027\u4ee5\u83b7\u5f97\u4ee3\u7801\u63d0\u793a\u5efa\u8bae\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u7ea6\u675f TS \u51fd\u6570\u53c2\u6570\u4e3a CSS \u5c5e\u6027\u4ee5\u83b7\u5f97\u4ee3\u7801\u63d0\u793a\u5efa\u8bae\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>ts \u7ea6\u675f\u5bf9\u8c61\u4e3a  \u5c5e\u6027<\/strong><\/p>\n<p>\u672c\u95ee\u9898\u65e8\u5728\u63a2\u8ba8\u5982\u4f55\u7ea6\u675f ts \u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a css \u5c5e\u6027\uff0c\u4ee5\u4fbf\u5728\u8c03\u7528\u51fd\u6570\u65f6\u83b7\u5f97\u4ee3\u7801\u63d0\u793a\u5efa\u8bae\u3002<\/p>\n<p>\u51fd\u6570\u5b9a\u4e49\u5982\u4e0b\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>\u4e3a\u5b9e\u73b0\u4ee3\u7801\u63d0\u793a\u5efa\u8bae\uff0c\u53ef\u4ee5\u5229\u7528 react \u63d0\u4f9b\u7684 cssproperties \u7c7b\u578b\u6216 csstype \u5e93\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u4f7f\u7528 react \u7684 cssproperties<\/strong><\/p>\n<p>\u5bf9\u4e8e react \u9879\u76ee\uff0c\u63a8\u8350\u4f7f\u7528 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><strong>\u4f7f\u7528 csstype \u5e93<\/strong><\/p>\n<p>\u5bf9\u4e8e\u975e react \u9879\u76ee\uff0c\u53ef\u4ee5\u5b89\u88c5 csstype \u5e93\u5e76\u4f7f\u7528\u5176 properties \u7c7b\u578b\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>\u901a\u8fc7\u7ea6\u675f\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a css \u5c5e\u6027\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u7f16\u8f91\u5668\u4e2d\u83b7\u5f97\u5c5e\u6027\u503c\u63d0\u793a\u5efa\u8bae\uff0c\u7b80\u5316\u5f00\u53d1\u4f53\u9a8c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u7ea6\u675f TS \u51fd\u6570\u53c2\u6570\u4e3a CSS \u5c5e\u6027\u4ee5\u83b7\u5f97\u4ee3\u7801\u63d0\u793a\u5efa\u8bae\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>ts \u7ea6\u675f\u5bf9\u8c61\u4e3a \u5c5e\u6027 \u672c\u95ee\u9898\u65e8\u5728\u63a2\u8ba8\u5982\u4f55\u7ea6\u675f ts \u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a css \u5c5e\u6027\uff0c\u4ee5\u4fbf\u5728\u8c03\u7528\u51fd\u6570\u65f6\u83b7\u5f97\u4ee3\u7801\u63d0\u793a\u5efa\u8bae\u3002 \u51fd\u6570\u5b9a\u4e49\u5982\u4e0b\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 \u4e3a\u5b9e\u73b0\u4ee3\u7801\u63d0\u793a\u5efa\u8bae\uff0c\u53ef\u4ee5\u5229\u7528 react \u63d0\u4f9b\u7684 cssproperties \u7c7b\u578b\u6216 csstype \u5e93\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4f7f\u7528 react \u7684 cssproperties \u5bf9\u4e8e react \u9879\u76ee\uff0c\u63a8\u8350\u4f7f\u7528 cssproperties \u7c7b\u578b\uff1a import { cssproperties } from &#8220;react&#8221;; function setattrstoelement(el: htmlelement, attr: cssproperties) [&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-26173","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26173","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=26173"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26173\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}