【问题标题】:Remove specific tag from innerHtml and render it on react page从 innerHtml 中删除特定标签并将其呈现在反应页面上
【发布时间】:2021-06-27 13:32:46
【问题描述】:

我使用 react 和 redux 创建了一个博客 Web 应用程序,在我的博客中有 4 个字段 id、title、seoName 和 description,对于在描述中的输入,我使用的是微型 Mce 文本编辑器 现在我想显示保存在 HTML 中的 Blog 类的描述字段中的文本 并使用椭圆截断它

这是存储在数据库中

    {
        "_id" : ObjectId("60640e75f7bafb14f6563d52"),
        "title" : "Improve Your Developer Experience With Nuxt Components",
        "seoName" : "improve-your-developer-experience-with-nuxt-components",
        "description" : "<p><img src=\"https://webconnect-upload.s3.amazonaws.com/160x00u0xj1ny179o3a1fd07kl26/screenshot-from-2021-03-31-11-22-39.png\" alt=\"\" width=\"802\" height=\"274\" /></p>\n<h2 id=\"introduction\">Introduction</h2>\n<p>The Nuxt team has introduced&nbsp;<strong><a href=\"https://www.npmjs.com/package/@nuxt/components\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">@nuxt/components</a></strong>&nbsp;module with the purpose to make Nuxt development faster and to make you, as a developer, more productive. This module comes with amazing features and options that will improve your development experience with Nuxt. No matter if you&rsquo;re just starting out or an advanced user,&nbsp;<a href=\"https://github.com/nuxt/components\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">@nuxt/components</a>&nbsp;provides a range of options from the simplest setup to advance configurations that will certainly benefit your projects.</p>\n<p>In a nutshell, this module automatically scans, imports and registers Vue components found in the&nbsp;<strong><code>~/components</code></strong>&nbsp;directory, so that we don't have to write import statements when we use them in either pages, layouts or even within components.</p>\n<p>&nbsp;</p>\n<blockquote>\n<p><em>This module parses your template and automatically includes the component in the file where you are using it such as a page, layout or even a component. Because Nuxt.js uses automatic code splitting to split your pages by default this module works perfect as it will only contain the components that are used on that page. Also, if you use a component in more than 2 pages, Nuxt.js will automatically create a shared chunk for them thanks to the magic of WebPack.</em></p>\n</blockquote>",
}

我只想显示截断的文本(从描述中删除图像) 其余内容请点击阅读更多。

现在我正在使用 建议有没有其他有效的方法

                  <p className="m-0px truncateBlogDesc"
                  dangerouslySetInnerHTML={{ __html: e.description }}
                  ></p>

【问题讨论】:

  • 我知道这不是您问题的答案。但是,你为什么不直接用 css 隐藏 truncateBlogDesc 中的 标签呢?
  • 您想要更高效还是更安全的东西? (或两者兼有?)如果您觉得使用 dangerouslySetInnerHTML 不安全,请不要使用它。你能澄清你在找什么吗?如果担心安全问题,您可以查看DOMPurify。听起来您还想截断原始 HTML 字符串。为此,您需要处理字符串并保留处理的标签堆栈,因此如果您在打开的标签中间截断,您可以关闭它。
  • 我用 DomPurify 解决了这个问题,谢谢 Drew :-) 另外请看这个问题@DrewReese stackoverflow.com/questions/66963413/…

标签: javascript reactjs innerhtml dangerouslysetinnerhtml


【解决方案1】:

您可以使用正则表达式替换字符串中的每个 标记 (e.description)。

例如:

var tmp = inner.replace(/<img .*?>/g,"<text>"); # remove <text> to replace the tag with an empty string ​

你可以试试:

​<p className="m-0px truncateBlogDesc" ​dangerouslySetInnerHTML={{ __html: e.description.replace(/<img .*?>/g,"") }} ​></p>

【讨论】:

  • 如果我只想显示

    标签是什么将是正则表达式(忽略所有标签并只显示

    标签)实际上我不知道如何编写正则表达式和这样的另一件事渲染innerHTML是否有效?

  • 不,我提供的正则表达式只是删除了字符串中的 标签(和里面的内容),它不会影响与 标签不匹配的任何其他字符串。我不确定这是做到这一点的“最佳方式”(我不是反应专家,所以也许我错了)但如果它可以很好地满足您的需求并且您的代码不需要进行很多优化(为了获得更好的性能)它会好的。只需仔细阅读dangerouslySetInnerHTML 文档以了解您在做什么(文档说您必须谨慎使用它)。
  • 好的,谢谢建议
  • 如果你可以隐藏除 p 标签之外的所有 html 标签,那么简单的 css 就可以了。你可以在你的元素上切换一个类 .readmore 。并使用类似: .readmore *:not(p) { display: none; }
【解决方案2】:

我用这个函数做了类似的事情。也许它会帮助你。此函数会触发所有 html 并仅返回文本。

const htmlparser = require('htmlparser2');

function stripTags(html) {
  let strippedHtml = '';
  // noinspection JSUnusedGlobalSymbols
  const parser = new htmlparser.Parser({
    ontext(text) {
      strippedHtml +=  text;
    },
  });
  parser.write(html);
  parser.end();

  return strippedHtml;
};

【讨论】:

  • html解析器是库吗?
  • 是的。但是,我认为它应该已经安装在你的 node_modules 中,作为其他包的依赖项。
猜你喜欢
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-23
  • 1970-01-01
  • 2014-01-11
  • 2023-03-21
相关资源
最近更新 更多