JavaScript是一门Web编程语言,用来实现网页的交互功能,它和HTML、CSS共同组成了个Web开发的基础工具集合,也是前端开发者必备的技能;学习JavaScript教程可以了解它在网页开发中的所有特性和相关概念,让我们能够更加快速的去开发Web应用。
这个indexOf() 方法返回从startIndex开始搜索 到的指定值的首个实例对象的 索引。若未找到该值,则返回-1。
版本实现
实现于JavaScript 1.0
语法说明
indexOf(searchValue, startIndex)
执行一下代码说明
searchValue:表示要搜索的值的字符串。
startIndex : 在字符串中搜索指定值的起始位置。该值为介于 0 到字符串长度之间的整数,默认值为 0。
字符串对象示例:indexOf() 方法
在以下网页文档中indexOf() 方法用于在给定数据中定位值。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript String object - indexOf() method example</title>
</head>
<body>
<h1 style="color: red">JavaScript String object : indexOf() method</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
var Str="Brave new world"
document.write("The index of the first w from the beginning is " +
Str.indexOf("w")+"<br />")
document.write("The index of 'new' from the beginning is " +
Str.indexOf("new"))
//]]>
</script>
</body>
</html>
执行一下支持的浏览器
IE浏览器7 | 火狐3.6 | 谷歌Chrome 7 | Safari 5.0.1 版 | Opera 10 |
Yes | Yes | Yes | Yes | Yes |
另请参阅:
上一篇:JavaScript fromCharCode() 方法:字符串对象
下一篇:JavaScript italics() 方法:字符串对象