当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML DOM Subscript用法及代码示例


HTML DOM中的下标对象用于表示HTML <sub>元素。可以使用getElementById()方法访问下标元素。

用法:

document.getElementById("id")

id分配给<sub>标签的位置。

范例1:

<!DOCTYPE html>  
<html>  
    <head> 
        <title> 
            HTML DOM subscript Object 
        </title> 
    </head> 
      
    <body style = "text-align:center"> 
           
        <h1 style = "color:green;" >  
            GeeksForGeeks  
        </h1>  
          
        <h2>DOM subscript Object</h2>  
          
        <p>A<sub id = "sub_scr">computer science</sub>  
        portal for geeks.</p> 
          
        <button onclick = "Geeks()"> 
            Click Here 
        </button> 
          
        <!-- script to set style to subscript object -->
        <script> 
            function Geeks() { 
                var txt = document.getElementById("sub_scr"); 
                txt.style.color = "green"; 
            } 
        </script> 
    </body>  
</html>                    

输出:
在单击按钮之前:
sub
单击按钮后:
sub



范例2:可以使用document.createElement方法创建下标对象。

<!DOCTYPE html>  
<html>  
    <head> 
        <title> 
            HTML DOM subscript Object 
        </title> 
    </head> 
      
    <body>  
        <center> 
        <h1 style = "color:green;" >  
            GeeksForGeeks  
        </h1>  
          
        <h2>DOM subscript Object</h2>  
  
        <button onclick = "Geeks()"> 
            Click Here! 
        </button> 
          
        <br><br> 
          
        <div> 
            A <span id = "p"></span> 
            portal for geeks. 
        </div> 
          
        <script> 
            function Geeks() { 
            var txt = document.createElement("SUB"); 
            var t = document.createTextNode("computer science"); 
            txt.appendChild(t); 
            document.getElementById("p").appendChild(txt); 
            } 
        </script> 
    </body>  
</html>                    

输出:
在单击按钮之前:
sub
单击按钮后:
sub




相关用法


注:本文由纯净天空筛选整理自Vishal Chaudhary 2大神的英文原创作品 HTML | DOM Subscript Object。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。