简体   繁体   English

为什么这个简单的代码在firefox中失败,但在包括Opera和旧IE版本在内的所有其他浏览器中都可以使用

[英]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.

相关问题 为什么此jQuery在IE中不起作用,但在其他所有浏览器中都起作用? - Why does this jQuery not work in IE but in every other browser? 为什么此代码在IE中可以运行,而在Firefox和Chrome中却无法运行? - Why would this code work in IE and fail in Firefox and Chrome? 我下面有一个代码-在IE和Opera中效果很好,但在Firefox / Netscape中根本不起作用。 有任何想法吗? - I have a code below - works great in IE and Opera, but does not work at all in Firefox / Netscape. Any ideas? 为什么我的网站可以在某些Chrome,Opera和Safari中使用,但不适用于FireFox或IE? - Why does my website work in some Chrome, Opera, and Safari but not FireFox or IE? 为什么我的Javascript / jQuery可以在Chrome和Safari中运行,但不能在Firefox,IE9或Opera中运行? - Why does my Javascript/jQuery work in Chrome and Safari but not firefox, IE9, or Opera? 为什么Safari / Opera无法与此JavaScript代码一起使用? - Why does Safari/Opera not work with this javascript code? 为什么我的代码在Safari或Opera中不起作用? - Why does my code not work in Safari or Opera? 为什么此代码在FireFox和IE中不能使用,而在Chrome中不能使用? - Why Doesn't This Code Work in FireFox and IE but does in Chrome? 为什么此jQuery代码在Chrome和Firefox中能正常工作,而在IE9上却不能工作? - Why does this jQuery code work fine in Chrome and Firefox, but not IE9? 如何在Firefox,Opera和IE中检测浏览器插件? - How to detect browser addons in Firefox, Opera and IE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM