源代码 :
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>开源教程(ossoft.cn)</title> </head> <body> <p id="p1">Click the button to locate where "locate" first occurs.</p> <p id="p2">0</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var str=document.getElementById("p1").innerHTML; var n=str.indexOf("locate"); document.getElementById("p2").innerHTML=n+1; } </script> </body> </html>
运行结果