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


HTML DOM Aside用法及代碼示例


DOM Aside對象用於表示HTML <Aside>元素。 Aside元素由getElementById()訪問。aside元素是html 5中的新增函數。語法:

document.getElementById("ID");

其中“id”是分配給aside標簽的ID。
示例1:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title>aside tag</title> 
    <style> 
        .gfg { 
            font-size:30px; 
        } 
          
        aside { 
            font-size:40px; 
            color:#090; 
            font-weight:bold; 
        } 
          
        p { 
            font-size:20px; 
            margin:20px 0; 
        } 
    </style> 
</head> 
  
<body> 
    //Assign ID to the aside tag. 
    <aside id="GFG"> 
        <h1>GeeksforGeeks</h1> 
        <p>A computer science portal for geeks</p> 
    </aside> 
      
    <h2 style="font-size:35px;">DOM Aside object</h2> 
    <button onclick="myGeeks()">Submit</buttom> 
        <script> 
            function myGeeks() { 
                var w = document.getElementById("GFG"); 
                w.style.color = "red"; 
            } 
        </script> 
</body> 
  
</html>                

輸出:
在單擊按鈕之前:

單擊按鈕後:

示例-2:可以使用document.createElement方法創建Aside Object。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title>aside tag</title> 
    <style> 
        .gfg { 
            font-size:30px; 
        } 
          
        p { 
            font-size:20px; 
            margin:20px 0; 
        } 
    </style> 
</head> 
  
<body> 
    <center> 
        <h2 style = "font-size:35px;">DOM Aside object</h2> 
        <button onclick="myGeeks()">Submit</buttom> 
            <script> 
                function myGeeks() { 
                    var w = document.createElement("ASIDE"); 
                    w.setAttribute("id", "GFG"); 
                    document.body.appendChild(w); 
  
                    var heading = document.createElement("H3"); 
                    var text1 =  
                    document.createTextNode("GeeksForGeeks"); 
  
                    heading.appendChild(text1); 
                    document.getElementById("GFG" 
                    ).appendChild(heading); 
  
                    var para = document.createElement("P"); 
                    var text2 = document.createTextNode( 
                    "A Computer Science Portal for Geeks."); 
                    
                    para.appendChild(text2); 
                    document.getElementById("GFG" 
                    ).appendChild(para); 
                } 
            </script> 
</body> 
  
</html>  

輸出:
在單擊按鈕之前:

單擊按鈕後:

支持的瀏覽器:下麵列出了DOM Aside Object支持的瀏覽器:

  • 穀歌瀏覽器
  • IE瀏覽器
  • Firefox
  • Opera
  • Safari




相關用法


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