Javascript 是什么导致错误“string.split不是函数”?
为什么我会 未捕获类型错误: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()以字符串形式返回位置,因此串联将触发该位置 您还可以使用来获取字符串。
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()
谢谢。我没有意识到我把变量从字符串转换成了对象。你的解决方案给了我一个检查代码的想法。