Javascript 未捕获的类型错误:.indexOf 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36483151/
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
Uncaught TypeError: .indexOf is not a function
提问by TerryO
I am new to JavaScript and I'm getting an erroras below.
我是 JavaScript 新手,出现如下错误。
Uncaught TypeError: time.indexOf is not a function
未捕获的类型错误:time.indexOf 不是函数
Gee, I really thought indexOf() really was a function. Here is a snippet of my code:
哎呀,我真的以为 indexOf() 真的是一个函数。这是我的代码片段:
var timeofday = new Date().getHours() + (new Date().getMinutes()) / 60;
document.getElementById("oset").innerHTML = timeD2C(timeofday);
</script>
<script>
function timeD2C(time) { // Converts 11.5 (decimal) to 11:30 (colon)
var pos = time.indexOf('.');
var hrs = time.substr(1, pos - 1);
var min = (time.substr(pos, 2)) * 60;
if (hrs > 11) {
hrs = (hrs - 12) + ":" + min + " PM";
} else {
hrs += ":" + min + " AM";
}
return hrs;
}
</script>
回答by Rajaprabhu Aravindasamy
Basically indexOf()
is a method belongs to string(array object also), But while calling the function you are passing a number, try to cast it to a string and pass it.
基本上indexOf()
是一个属于字符串的方法(也是数组对象),但是在调用传递数字的函数时,尝试将其转换为字符串并传递它。
document.getElementById("oset").innerHTML = timeD2C(timeofday + "");
var timeofday = new Date().getHours() + (new Date().getMinutes()) / 60;
function timeD2C(time) { // Converts 11.5 (decimal) to 11:30 (colon)
var pos = time.indexOf('.');
var hrs = time.substr(1, pos - 1);
var min = (time.substr(pos, 2)) * 60;
if (hrs > 11) {
hrs = (hrs - 12) + ":" + min + " PM";
} else {
hrs += ":" + min + " AM";
}
return hrs;
}
alert(timeD2C(timeofday+""));
And it is good to do the string conversion inside your function definition,
最好在函数定义中进行字符串转换,
function timeD2C(time) {
time = time + "";
var pos = time.indexOf('.');
So that the code flow won't break at times when devs forget to pass a string into this function.
这样当开发人员忘记将字符串传递给此函数时,代码流不会中断。
回答by brk
Convert timeofday to string to use indexOf
将 timeofday 转换为要使用的字符串 indexOf
var timeofday = new Date().getHours() + (new Date().getMinutes()) / 60;
console.log(typeof(timeofday)) // for testing will log number
function timeD2C(time) { // Converts 11.5 (decimal) to 11:30 (colon)
var pos = time.indexOf('.');
var hrs = time.substr(1, pos - 1);
var min = (time.substr(pos, 2)) * 60;
if (hrs > 11) {
hrs = (hrs - 12) + ":" + min + " PM";
} else {
hrs += ":" + min + " AM";
}
return hrs;
}
// "" for typecasting to string
document.getElementById("oset").innerHTML = timeD2C(""+timeofday);
Solution 2
解决方案2
use toString()
to convert to string
用于toString()
转换为string
document.getElementById("oset").innerHTML = timeD2C(timeofday.toString());
回答by Andrew Grothe
I ran across this error recently using a javascript library which changes the parameters of a function based on conditions.
我最近使用 javascript 库遇到了这个错误,该库根据条件更改函数的参数。
You can test an object to see if it has the function. I would only do this in scenarios where you don't control what is getting passed to you.
您可以测试一个对象以查看它是否具有该功能。我只会在您无法控制传递给您的内容的情况下执行此操作。
if( param.indexOf != undefined ) {
// we have a string or other object that
// happens to have a function named indexOf
}
You can test this in your browser console:
您可以在浏览器控制台中对此进行测试:
> (3).indexOf == undefined;
true
> "".indexOf == undefined;
false