确定文本在字符串中首次出现的位置 - indexOf()

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript 字符串方法</h1>
  6.  
  7. <p>indexOf() 方法返回指定文本首次出现的位置:</p>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. var str = "The full name of China is the People's Republic of China.";
  13. var pos = str.indexOf("China");
  14. document.getElementById("demo").innerHTML = pos;
  15. </script>
  16.  
  17. </body>
  18. </html>