含义:
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。(工作中常用)
提示和注释:
注释:indexOf() 方法对大小写敏感!
注释:如果要检索的字符串值没有出现,则该方法返回 -1。
实例
我们将在 "Hello world!" 字符串内进行不同的检索:
<script type="text/javascript"> ???var str="Hello world!"; ?document.write(str.indexOf("Hello") + "<br />"); ?document.write(str.indexOf("World") + "<br />"); document.write(str.indexOf("world")); ???</script>
以上代码的输出:
0 ?-1 ?6 ??
多数用途
可以用于邮箱是否正确的判断,例如
<script type="text/javascript"> ???if((yx.email.value.indexOf(‘@‘,0)==-1)||(yx.email.value.indexOf(‘.‘,0)==-1)){alter("邮箱地址错误");yx.email.focus();return false;}</script>
另一个例子
在vue中的props
// 更好的做法!props: { ?status: { ???type: String, ???required: true, ???validator: function (value) { ?????return [ ???????‘syncing‘, ???????‘synced‘, ???????‘version-conflict‘, ???????‘error‘ ?????].indexOf(value) !== -1 ???} ?}}
js index of()用法
原文地址:https://www.cnblogs.com/DZzzz/p/9744626.html