【问题标题】:Display html markup in react jsx在反应jsx中显示html标记
【发布时间】:2019-08-18 06:00:54
【问题描述】:

想要在我的模板中直接显示 html 标记。

这是我正在编写要显示的 html 代码的文件。我想显示所有的 html 元素。

import React from 'react';

const html = (
  <div>
      <ul>
         <li>1</li>
         <li>2</li>
         <li>3</li>
      </ul>
  </div>
);

export default html

组件文件

import React, { Component } from 'react';
import html from './code/htmlCodeSnippet';

...
  render() {
    return (
      <div>
        {html}
      </div>
    )
  }
}

我正在导入要在我的组件中显示的 html 文件。我将使用 html 荧光笔来显示代码。

每当我引用{html} 时,它都不会显示所有的html 元素。它只是显示为

1
2
3

【问题讨论】:

  • 你怎么确定它没有显示 div 标签?
  • @lakerskill 你是什么意思?因为当我查看页面时,没有显示任何元素。

标签: javascript html reactjs react-redux jsx


【解决方案1】:

您需要进行一些更改。首先将 html 更改为字符串,即

const html = `
   <div>
      <ul>
         <li>1</li>
         <li>2</li>
         <li>3</li>
         <li>4</li>
         <li>5</li>
      </ul>
      <p>hello</p>
  </div>`;

并使用precode 元素将html 包装在渲染方法中,即

&lt;pre&gt;&lt;code&gt;{html}&lt;/code&gt;&lt;/pre&gt;

【讨论】:

    猜你喜欢
    • 2020-06-23
    • 1970-01-01
    • 2020-06-18
    • 2021-08-01
    • 2012-11-02
    • 2021-11-06
    • 2021-07-14
    • 2018-05-28
    • 2021-11-19
    相关资源
    最近更新 更多