[英]What is causing the error `string.split is not a function`?
為什么我越來越...
未捕獲的類型錯誤:string.split 不是函數
...當我跑...
var string = document.location; var split = string.split('/');
改變這...
var string = document.location;
到這...
var string = document.location + '';
這是因為document.location
是一個Location 對象。 默認.toString()
以字符串形式返回位置,因此連接將觸發該位置。
您還可以使用document.URL
來獲取字符串。
可能是
string = document.location.href;
arrayOfStrings = string.toString().split('/');
假設您想要當前的網址
運行這個
// you'll see that it prints Object
console.log(typeof document.location);
你想要document.location.toString()
或document.location.href
document.location
不是字符串。
您可能想改用document.location.href
或document.location.pathname
。
在子句 if 中,使用()
。 例如:
stringtorray = "xxxx,yyyyy,zzzzz";
if (xxx && (stringtoarray.split(',') + "")) { ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.