大佬求解!
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>string對(duì)象 </title>
<script type="text/javascript">
? var mystr="Hello World!"
? var sencond = mystr.indexof("o");
? document.write("第一次位置:"+sencond+"<br />");
? document.write("第二次位置:"+mystr.indexof("o",sencond));
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>string對(duì)象 </title>
<script type="text/javascript">
? var mystr="Hello World!"
? var sencond = mystr.indexof("o");
? document.write("第一次位置:"+sencond+"<br />");
? document.write("第二次位置:"+mystr.indexof("o",sencond));
</script>
</head>
<body>
</body>
</html>
2019-10-07
首先代碼就出現(xiàn)問(wèn)題了mystr.indexOf()中Of是大小寫的,你的是全小寫,還有你第一次獲取的位置如果直接用在第二次的位置上會(huì)出現(xiàn)重疊現(xiàn)象,導(dǎo)致兩次的結(jié)果一樣,所以要在第一次獲取位置的基礎(chǔ)上+1,也就是document.write("第二次位置:"+mystr.indexOf("o",sencond+1));這樣就行了