[英]Why does this simple code fail in firefox yet work in every other browser including opera and old IE versions
function T(x){ return (x.textContent) ? function(y){ x.textContent = y; } : function(y){ x.innerText = y; }; }
T(nodeA)('string');
nodeText = T(nodeB);
nodeText('string');
If I change (x.textContent)
to (x.textContent !== undefined)
it works in firefox. 如果我将
(x.textContent)
更改为(x.textContent !== undefined)
它将在Firefox中工作。 Otherwise I get no errors but nothing happens. 否则我没有任何错误,但是什么也没有发生。 Inspecting with firebug shows that
T(node);
用萤火虫检查表明
T(node);
returns function()
, which is just baffling to me. 返回
function()
,这让我感到困惑。
I'm new to javascript but I'm thinking this could be a bug?? 我是javascript新手,但我认为这可能是一个错误? I think it can only be true or false, it should be true and return first function but it doesn't return either.
我认为它只能为true或false,应该为true并返回第一个函数,但不会返回任何一个。 Can someone say why?
有人可以说为什么吗?
This won't work if the textContent
for the given node is an empty string ''
, which evaluates to false
. 如果给定节点的
textContent
是一个空字符串''
,它的值为false
,这将无法工作。 That's why you should do (typeof x.textContent !== 'undefined')
instead to ensure the existence of the property. 这就是为什么您应该这样做
(typeof x.textContent !== 'undefined')
来确保该属性的存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.