Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是什么导致错误“string.split不是函数”?_Javascript_Jquery_Split - Fatal编程技术网

Javascript 是什么导致错误“string.split不是函数”?

Javascript 是什么导致错误“string.split不是函数”?,javascript,jquery,split,Javascript,Jquery,Split,为什么我会 未捕获类型错误:string.split不是函数 …当我跑的时候 var string=document.location; var split=string.split('/')更改此 var string = document.location; 对此 var string = document.location + ''; 这是因为document.location是一个。默认的.toString()以字符串形式返回位置,因此串联将触发该位置 您还可以使用来获取字符串。

为什么我会

未捕获类型错误:string.split不是函数

…当我跑的时候

var string=document.location;
var split=string.split('/')更改此

var string = document.location;
对此

var string = document.location + '';

这是因为
document.location
是一个。默认的
.toString()
以字符串形式返回位置,因此串联将触发该位置


您还可以使用来获取字符串。

可能吧

string = document.location.href;
arrayOfStrings = string.toString().split('/');

假设您需要当前url
文档。位置不是字符串

您可能想改用
document.location.href
document.location.pathname

运行此命令

// you'll see that it prints Object
console.log(typeof document.location);
您需要
document.location.toString()
document.location.href
在第条中,如果,请使用
()
。 例如:

stringtorray = "xxxx,yyyyy,zzzzz";
if (xxx && (stringtoarray.split(',') + "")) { ...

文档。位置是一个对象。请尝试:
var string=document.location.href
lol.4个答案(至少)同时回答。我不应该看最新的问题,所以:)调用
toString()
而不是hacky concatenation不是更干净吗?@bažmegakapa:是的,这是一个偏好问题。
+'
是一种非常常见的字符串强制技巧,但是有些人更喜欢
toString()
方法。我不认为它比使用一元代码>代码>数字转换更糟糕。有
parseInt()
parseFloat()
。还有
Number()
+
当然较短,但对于不习惯破解代码或经验不足的人来说可读性较差。
+'
方法对我在Chrome浏览器中没有任何改变,但
toString()
确实有。@MA Maddin:你做了
我的字符串+”.split()
?如果是这样,您需要使用paren,因为
+
的优先级低于
。这样:
(我的字符串+).split()
谢谢。我没有意识到我把变量从字符串转换成了对象。你的解决方案给了我一个检查代码的想法。