带有Firefox innerText问题的Javascript

sly*_*dan 3 javascript firefox

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

这个函数在firefox中打开一个参数为undefined的页面,在google chrome中我获得了适当的值.

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

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

c69*_*c69 14

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

所以,虽然常识是使用:

var toUsername = anchor.innerText || anchor.textContent;
Run Code Online (Sandbox Code Playgroud)

或者某种包装器,只使用.text你正在使用的其他库中的jQuery 或其模拟器可能更聪明.