运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>The Element Object</h1> <h2>The Differences Between innerText, innerHTML and textContent</h2> <p id="myP"> This element has extra spacing and contains <span>a span element</span>.</p> <button onclick="getinnerHTML()">Get innerHTML</button> <button onclick="getinnerText()">Get innerText</button> <button onclick="gettextContent()">Get textContent</button> <pre id="demo"></pre> <script> function getinnerText() { let text = document.getElementById("myP").innerText; document.getElementById("demo").innerText = text; } function getinnerHTML() { let text = document.getElementById("myP").innerHTML; document.getElementById("demo").innerText = text; } function gettextContent() { let text = document.getElementById("myP").textContent; document.getElementById("demo").innerText = text; } </script> </body> </html>