时间:2021-07-01 10:21:17 帮助过:135人阅读

我们首先来看一下indexof的基本语法
string.indexOf(searchValue[, fromIndex])
searchValue表示要搜索的值的字符串。
fromIndex表示调用字符串中用于开始搜索的位置。它可以是0到字符串长度之间的任何整数,默认值为0。
从fromIndex开始搜索,如果未找到该值则返回-1。
我们来看具体的示例
代码如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type = "text/javascript">
var str1 = new String( "This is string one" );
var index = str1.indexOf( "string" );
document.write("indexOf found String :" + index );
document.write("<br />");
var index = str1.indexOf( "one" );
document.write("indexOf found String :" + index );
</script>
</body>
</html>浏览器上显示效果如下

本篇文章到这里就全部结束了,关于index()方法更加详细的用法大家可以参考JavaScript中indexof如何使用这篇文章!!!
以上就是indexof方法怎么使用的详细内容,更多请关注Gxl网其它相关文章!