编辑下面的代码:【加编码】
<html> <body> <p id="p1">Click the button to locate where "locate" first occurs.</p> <p id="p2">0</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var str=document.getElementById("p1").innerHTML; var n=str.indexOf("locate"); document.getElementById("p2").innerHTML=n+1; } </script> </body> </html>
结果: 【此窗口】 帮助?
Try it Yourself - © 自强学堂