Javascript textContent 在 IE8 或 IE7 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18326689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:29:56  来源:igfitidea点击:

Javascript textContent is not working in IE8 or IE7

javascripthtmlcssinternet-explorer

提问by logan

I need to add 2 cell content of a table and display it. Below JavaScript command works fine in chrome or IE10. But not working in IE8 or 7.

我需要添加表格的 2 个单元格内容并显示它。下面的 JavaScript 命令在 chrome 或 IE10 中工作正常。但不适用于 IE8 或 7

parseFloat(document.getElementById("total").textContent).toFixed(2);

It results,

结果,

NaN

NaN

Could you please tell me what is the equivalent command in IE7 or IE8to read cell content of a table and convert it to float then add..

你能告诉我IE7 或 IE8中读取表格单元格内容并将其转换为浮点数然后添加..

回答by Unknown

textContentis not supported by IE7/8. The latter has a different property called innerText which returns the text contents of a DOM node.

IE7/8 不支持textContent。后者有一个名为 innerText 的不同属性,它返回 DOM 节点的文本内容。

Here is how to use both:

以下是两者的使用方法:

var text  = e.item.textContent || e.item.innerText;
alert(text);

NOTE:e is html element

注意:e 是 html 元素

回答by DrewB

Unknowns answer is completely correct. That said if you are using jQuery, you can simply do $(element).text()

未知答案是完全正确的。也就是说,如果你使用 jQuery,你可以简单地做$(element).text()