替换字符串中的字符 - replace()
- <!DOCTYPE html>
- <html>
- <body>
- <h1>JavaScript 字符串方法</h1>
- <p>请把下面的段落中的“Microsoft”替换为“baidu”:</p>
- <button onclick="myFunction()">试一试</button>
- <p id="demo">请访问 Microsoft!</p>
- <script>
- function myFunction() {
- var str = document.getElementById("demo").innerHTML;
- var txt = str.replace("Microsoft","baidu");
- document.getElementById("demo").innerHTML = txt;
- }
- </script>
- </body>
- </html>