源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>享学课堂</title> </head> <body> <p id="p1">Please locate where 'locate' occurs!.</p> <button onclick="myFunction()">试试</button> <p id="demo"></p> <script> function myFunction() { var str = document.getElementById("p1").innerHTML; var pos = str.search("locate"); document.getElementById("demo").innerHTML = pos; } </script> </body> </html>
运行结果