是什么原因导致了以下错误:javascript中的“string.split不是一个函数”?

下面显示了以下JavaScript代码:

<script type="text/javascript">

$(document).ready(function() { var string = document.location; var string2 = string.split('/'); }); </script>

运行此代码时,在Firebug控制台中显示以下错误:

string.split is not a function
var string2 = string.split('/');

造成这个错误的原因是什么?

改变这个

var string = document.location;

变成这个

var string = document.location + ''; 

这是因为document.location是一个Location对象。默认情况下.toString()以字符串形式返回位置,所以连接会触发该位置。

你也可以document.URL用来获取一个字符串。

猜你喜欢

转载自www.cnblogs.com/qianzf/p/12556124.html