简体   繁体   English

如何将跨度元素插入 object(字符串)?

[英]How to insert a span element into an object (string)?

I am working with styled components.我正在使用样式化的组件。 I accept an h1 element as a prop from an object which contains a string called title .我接受一个 h1 元素作为来自 object 的道具,其中包含一个名为title的字符串。 How can I insert a span element into the string of the object so I could style only this part separately as you would normally do if the text was hardcoded?如何将 span 元素插入 object 的字符串中,这样我就可以像硬编码文本时通常那样单独设置这部分的样式?

export const heroData = {
  title: `Finance Data`,

You can use dangerouslySetInnerHTML attribute for adding raw HTML string to HTML.您可以使用dangerouslySetInnerHTML属性将原始 HTML 字符串添加到 HTML。

export const heroData = {
  title: "Finance <span style="color: red;">Data</span>"
... ...

<div dangerouslySetInnerHTML={{_html: heroData.title}} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM