【问题标题】:How to get html text of a div [closed]如何获取 div 的 html 文本 [关闭]
【发布时间】:2021-08-05 21:48:58
【问题描述】:

我有一个字符串2 × 10,它在 div 中使用 dangerouslySetInnerHTML 渲染;当用户单击该 div 时,我想获取文本 2 × 10,但使用 event.target.innerHTML 我得到 2 x 10"2 × 10" 呈现为 2 x 10),有办法获取原始的 dangerouslySetInnerHTML?

【问题讨论】:

  • elm.textContent 会起作用,如果有的话。
  • 跟reactjs有什么关系?

标签: javascript html jsx


【解决方案1】:

尝试使用outerHTML:event.target.outerHTML

【讨论】:

    【解决方案2】:

    HTML 用这样的函数再次对其进行编码:

    function htmlEntities(str) {
        return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
    }
    

    感谢 j08691 的回答 here

    【讨论】:

      猜你喜欢
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 2011-05-20
      • 2015-06-30
      • 2021-12-18
      • 1970-01-01
      相关资源
      最近更新 更多