javascript - Firefox 将注释文本保存在 style 元素内的 textContent 中

标签 javascript html firefox dom frontend

我有以下 div

<div id="test"><style type="text/css"><!-- comment --></style><span>text</span></div>

我需要检索它的可见文本内容,我正在使用 textContent像这样的属性:

document.getElementById("text").textContent

它在 Chrome 中工作正常,但是 Firefox 突然返回 <!-- comment -->text 。我知道,我可以通过使用一个临时 div ( document.createElement ) 来修复它,但是,也许有更好的方法?

fiddle 链接:https://jsfiddle.net/e28m64tf/

更新:

看来这段代码在 Chrome 中也返回了错误的结果。我的错。

最佳答案

您在 Google Chrome 中也会得到相同的结果。要解决此问题,您可以尝试以下操作:

alert(
  document.getElementById("test").
  textContent.
  replace(/<!--(.*?)-->/gm, '')
);
<div id="test">
  <style type="text/css"><!-- comment --></style>
  <span>text</span>
</div>

关于javascript - Firefox 将注释文本保存在 style 元素内的 textContent 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36871502/

相关文章:

javascript - javascript中的变量范围

javascript - Fullcalendar 禁用拖放但启用可编辑

javascript - React Native - 应用程序在运行和应用 "setInterval"后崩溃

javascript - 如何从传递的表中获取行数?

html - 如何从该按钮的中间绘制一条垂直线?

javascript - jquery 每次循环dom操作(JSON)

html - 将屏幕中间的两个 div 与其中的图像对齐

javascript - Firefox 4 中的 AjaxForm 文件上传

android - 目标 Firefox 桌面 CSS

html - Firefox - 宽度 : 100% not working for list in a list (display: table;)