带有 Firefox innerText 问题的 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7461598/
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-26 00:12:02  来源:igfitidea点击:

Javascript with Firefox innerText issue

javascriptfirefox

提问by sly_Chandan

function OpenWindow(anchor) {
        var toUsername = anchor.innerText;
        window.open("ChatWindow.aspx?username=" + toUsername,'_blank', "width=340,height=200");       
    }

this function opens up a page with parameter as undefined in firefox where as in google chrome I get proper value.

此函数打开一个页面,参数在 firefox 中未定义,而在 google chrome 中,我得到了正确的值。

Firefox url: http://localhost:9452/ChatWindow.aspx?username=undefined

火狐网址: http://localhost:9452/ChatWindow.aspx?username=undefined

What is the solution for this issue?

这个问题的解决方案是什么?

回答by c69

While innerTextis non-standard, it significantly differs from textContent, because first one is doing pretty printing(for example, <br/>are converted to new lines), while second one - is not.

虽然innerText是非标准的,但它与 显着不同textContent,因为第一个正在做漂亮的打印(例如,<br/>转换为新行),而第二个 - 不是。

So, while common wisdom is to use:

因此,虽然常识是使用:

var toUsername = anchor.innerText || anchor.textContent;

or some kind of wrapper, it can probably be smarter to just use jQuery's .textor its analog from other library you are using.

或某种包装器,仅使用 jQuery.text或您正在使用的其他库中的类似物可能会更聪明。

回答by Davide Piras

try to change anchor.innerTextwith:

尝试改变anchor.innerText

anchor.textContent

this hopefully works in all browsers.

这希望适用于所有浏览器。

also see here: 'innerText' works in IE, but not in Firefox

另见此处:“innerText”适用于 IE,但不适用于 Firefox

P.S.I really reccomend using JQuery to avoid these kind of issues and to be sure to always write fully cross-browser javascript.

PS我真的建议使用 JQuery 来避免这些问题,并确保始终编写完全跨浏览器的 javascript。

回答by kzh

innerTextis a Microsoft invention whereas textContentis a W3C standard.

innerText是 Microsoft 的发明,而textContent是 W3C 标准。

function OpenWindow(anchor) {
    var toUsername = anchor.textContent || anchor.innerText || '';
    window.open("ChatWindow.aspx?username=" + toUsername,'_blank', "width=340,height=200");       
}

This should work. MooTools or some other JavaScript framework should be able to help with cross-browser inconsistencies.

这应该有效。MooTools 或其他一些 JavaScript 框架应该能够帮助解决跨浏览器的不一致问题。

回答by cgsabari

use textContent for innerText. example

对innerText 使用textContent。例子

<script>
function change()
{
   document.getElementById("label").textContent="Hello";
}
</script>

it will work on ff. and chrome too. but not work on IE.

它将在 ff 上工作。还有铬。但不适用于 IE。

回答by Amrit Pal Singh

I was getting the same problem because Firefox does not support the innerText property, instead, it supports the textContent property. so check for the browser's feature support to use the correct property accordingly.

我遇到了同样的问题,因为 Firefox 不支持 innerText 属性,而是支持 textContent 属性。因此,请检查浏览器的功能支持以相应地使用正确的属性。

if(document.all){
 document.getElementById('element').innerText = "myText";
} else{
document.getElementById('element').textContent = "myText";
}

or it's better to use Jquery to resolve cross browser issues. Usage:

或者最好使用 Jquery 来解决跨浏览器问题。用法:

$('element').text("myText");