简体   繁体   English

在 div 中添加一个 span 并使用 dangerouslySetInnerHTML 来显示工具提示

[英]Add a span inside a div with dangerouslySetInnerHTML for showing a tooltip

There is a div element which has a link to a page set like this:有一个 div 元素,它有一个指向这样的页面集的链接:

  <a href={row.original.productURL}>
    <div className="product-sku">
      <p>{row.values.sku}</p>
      <div
        className="product-details"
        dangerouslySetInnerHTML={{
          __html: row.original.description
        }}>
      </div>
    </div>
  </a>

it works fine but I want to add a tooltip to it when hover, so I tried to add a span inside the div:它工作正常,但我想在 hover 时向它添加一个工具提示,所以我尝试在 div 内添加一个跨度:

  <a href={row.original.productURL}>
    <div className="product-sku">
      <p>{row.values.sku}</p>
      <div
        className="tooltip product-details"
        dangerouslySetInnerHTML={{
          __html: row.original.description
        }}>
        <span className="tooltiptext">This is my tooltip</span>
      </div>
    </div>
  </a>

It seems to be forbidden as it returns an error:它似乎被禁止,因为它返回错误:

Uncaught Error: Can only set one of children or props.dangerouslySetInnerHTML .未捕获的错误:只能设置childrenprops.dangerouslySetInnerHTML

Is there a way to add a tooltip in this case?在这种情况下有没有办法添加工具提示?

A bit late for what you asked, however if someone else is having this issue... you just have to simply append your HTML to the __html prop.你问的有点晚了,但是如果其他人有这个问题......你只需要简单地 append 你的 HTML 到 __html 道具。

Basically this:基本上是这样的:

<div
    className="tooltip product-details"
    dangerouslySetInnerHTML={{
      __html: row.original.description
    }}>
    <span className="tooltiptext">This is my tooltip</span>
</div>

becomes this:变成这样:

<div
    className="tooltip product-details"
    dangerouslySetInnerHTML={{
      __html: row.original.description + `<span class="tooltiptext">This is my tooltip</span>`
    }}>
</div>

This allows you to nest div/span classes within a div which uses the dangerouslySetInnerHTML prop.这允许您在使用 dangerouslySetInnerHTML 属性的 div 中嵌套 div/span 类。

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

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