2011-03-07 52 views

回答

5

split()方法用于将字符串拆分为一个子字符串数组,并返回新数组。因此,[1]表示分割阵列的第二元件window.location.hash.split("#")[1];

JavaScript Split Function

2

它访问从分割中找到的第二个元素。

3
var hashString = "#it #is #easy #to #understand #arrays"; 

/* 
hashString.split("#")[0] = "" 
hashString.split("#")[1] = "it " 
hashString.split("#")[2] = "is " 
hashString.split("#")[3] = "easy " 
hashString.split("#")[4] = "to " 
hashString.split("#")[5] = "understand " 
hashString.split("#")[6] = "arrays" 
*/ 

之所以分裂(“#”)[0]是一个空字符串,因为分割函数遇到一个“# “在字符串的最开始处,此时它会创建一个数组入口,其中包括迄今为止所传递的每个字符,但”#“除外。由于它迄今没有传递任何字符,因此会创建一个空字符串的条目。

下面是另一个示例:

var hashString = "it #is #easy #to #understand #arrays"; 

/* 
hashString.split("#")[0] = "it " 
hashString.split("#")[1] = "is " 
hashString.split("#")[2] = "easy " 
hashString.split("#")[3] = "to " 
hashString.split("#")[4] = "understand " 
hashString.split("#")[5] = "arrays" 
*/ 
2

剥掉散列(#)是一个简单的方法...

var hash = window.location.hash.substr(1); 
1

split()返回一个数组,[1]抓住第二元件阵列中的[0]会抓住第一个元素。