English 中文(简体)
html 元素未执行
原标题:html element not being executed

每当我试图运行到代码线下, 它不执行 < br/ > 作为断线, 但实际上执行为字符串 。

const firstFiveLetters = chartData.centerText.slice(0, 5);
const remainingLetters = chartData.centerText.slice(5);

// Return the text with line break after the first five letters
return `${firstFiveLetters}<br>${remainingLetters}`;

我期待执行第一个但不能执行的 html 元素后出现断线。

问题回答

我认为问题在于 您没有将 < code> fifelletters 转换为 HTML, 所以它被检测成字符串 ;

const firstFiveLetters = chartData.centerText.slice(0, 5);
const remainingLetters = chartData.centerText.slice(5);
//Render the variable with the desired content as HTML using __html:
const htmlString = `${firstFiveLetters}<br>${remainingLetters}`;
return { __html: htmlString };

您的方法返回字符串, 而不是反应元素, 这就是为什么线断转换为字符串。 格式化文本的重新反应方式将看起来类似 :

const firstFiveLetters = chartData.centerText.slice(0, 5);
const remainingLetters = chartData.centerText.slice(5);

return (<>{firstFiveLetters}<br />{remainingLetters}</>);

您可以在此情况下看到, 我们将返回 React 节点, 而不是字符串。 您甚至可以为这种格式化创建组件 。

function LineBreakTextFormat({ text, first = 5 }) {
  const start = text.slice(0, first);
  const remaining = text.slice(first);
  return (
    <>
     {start}
     {!!remaining && <><br />{remaining}</>}
    </>
  );
}

// And then use it like this
<LineBreakTextFormat text={chartData.centerText} first={5} />




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签