javascript IE - 无法获得财产

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

IE - Unable to get property

javascriptinternet-explorer

提问by bnorton

I am running into an error in IE 9 and 10. The error is "Unable to get property 'replace' of undefined or null reference". If we are to believe Microsoft's error, it reports the error is occurring on the value.replace line of the following function:

我在 IE 9 和 10 中遇到错误。错误是“无法获取未定义或空引用的属性‘替换’”。如果我们相信 Microsoft 的错误,它会报告错误发生在以下函数的 value.replace 行上:

function jsstrtonum($value) {
    if($value == '') {
        $value = '0.00';
    }
    var $retval = $value.replace(/[^0-9\.]+/g, '');

    return Number($retval);
}

I am only having difficulty in IE. All other browsers execute this function flawlessly. I have been working on this for hours now. Any help would be greatly appreciated.

我只是在 IE 中遇到困难。所有其他浏览器都可以完美地执行此功能。我已经为此工作了几个小时。任何帮助将不胜感激。

采纳答案by algorhythm

try

尝试

$value = new String($value);

before using 'replace'.

在使用“替换”之前。

回答by machineghost

As Rocket Hazmat suggested, the problem is most likely that $value is something other than a string. One quick and dirty approach you could use to debug what it is is:

正如 Rocket Hazmat 所建议的那样,问题很可能是 $value 不是字符串。您可以使用一种快速而肮脏的方法来调试它是什么:

function jsstrtonum($value) {
    if (!$value.replace) {
        alert($value);
        // Alternatively if the IE developer tool gives you a console you could:
        // console.log($value);
    }
}

This way you can see what $valueis, and hopefully debug your problem from there.

这样您就可以看到是什么$value,并希望从那里调试您的问题。

回答by Lee Meador

Might as well just replace the condition with:

不妨将条件替换为:

if (!$value)

So that all the nulls and undefined values turn into the 0.00 string.

这样所有的空值和未定义的值都变成了 0.00 字符串。