源代碼
點擊運行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>教程</title> </head> <body> <p>單擊按鈕將段落中的第一個 Microsoft 替換成 Runoob:</p> <p id="demo">Visit Microsoft!Visit Microsoft!</p> <button onclick="myFunction()">點我</button> <script> function myFunction(){ var str=document.getElementById("demo").innerHTML; var n=str.replace("Microsoft","Runoob"); document.getElementById("demo").innerHTML=n; } </script> </body> </html>
運行結果