當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


HTML DOM innerText用法及代碼示例

DOM innerText屬性用於設置或返回指定節點及其後代的文本內容。此屬性與文本內容屬性非常相似,但返回除<script>和<style>元素之外的所有元素的內容。

用法:它用於設置innerText屬性。

node.innerText = text 

返回值:它返回一個字符串值,該值代表元素及其後代的文本內容。
示例1:


<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM innerText Property 
    </title> 
</head> 
  
<body> 
    <h1>GeeksforGeeks</h1> 
    <h2>HTML DOM textContent Property</h2> 
  
    <button id="geeks" onclick="MyGeeks()"> 
        Submit 
    </button> 
  
    <p id="sudo"></p> 
    <script> 
        function MyGeeks() { 
            var text = 
                document.getElementById("geeks").innerText; 
            document.getElementById("sudo").innerHTML = text; 
        } 
    </script> 
</body> 
  
</html>         

輸出:
在單擊按鈕之前:

單擊按鈕後:

示例2:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM innerText Property 
    </title> 
</head> 
  
<body> 
    <center> 
        <h1>GeeksforGeeks</h1> 
        <h2>HTML DOM innerText Property</h2> 
        <p id="geeks" style="font-size:20px;">Hello Geeks!</p> 
        <button onclick="MyGeeks()"> 
            Submit 
        </button> 
        <!-- MyGeeks function replace the inner text-->
        <script> 
            function MyGeeks() { 
                document.getElementById("geeks").innerText = 
                                  "Welcome to GeeksforGeeks!"; 
            } 
        </script> 
    </center> 
</body> 
  
</html>      

輸出:
在單擊按鈕之前:

單擊按鈕後:

支持的瀏覽器:下麵列出了DOM innerText屬性支持的瀏覽器:

  • 穀歌瀏覽器1.0
  • Internet Explorer 4.0
  • Firefox 1.0
  • Opera 3.5
  • Safari 1.0


相關用法


注:本文由純淨天空篩選整理自ManasChhabra2大神的英文原創作品 HTML | DOM innerText Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。