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


HTML DOM ColumnGroup用法及代码示例


HTML DOM中的ColumnGroup对象用于表示HTML <colgroup>元素。此标记用于设置或获取<colgroup>元素的属性。可以使用getElementById()方法进行访问。

用法:

document.getElementById("ColGroup_ID");

此ColGroup_ID分配给HTML <colgroup>元素。

属性值:

  • span:用于设置或返回span属性的值。

示例1:使用document.getElementById(“myColGroup”).id返回“ColumnGroup id”;



<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM ColumnGroup Object 
    </title> 
</head> 
<style> 
    #myColGroup { 
        background:green; 
    } 
      
    table { 
        color:white; 
        margin-left:180px; 
        background:yellow; 
    } 
      
    #Geek_p { 
        color:green; 
        font-size:30px; 
    } 
      
    td { 
        padding:10px; 
    } 
</style> 
  
<body style="text-align:center;"> 
  
    <h1 style="color:green;">   
            GeeksForGeeks   
        </h1> 
  
    <h2>HTML | DOM ColumnGroup Object</h2> 
    <table> 
        <colgroup id="myColGroup" span="2"> 
        </colgroup> 
        <tr> 
            <th>S.No</th> 
            <th>Title</th> 
            <th>Geek_id</th> 
        </tr> 
        <tr> 
            <td>Geek_1</td> 
            <td>GeekForGeeks</td> 
            <th>Geek_id_1</th> 
        </tr> 
        <tr> 
            <td>Geek_2</td> 
            <td>GeeksForGeeks</td> 
            <th>Geek_id_2</th> 
        </tr> 
    </table> 
    <br> 
    <button onclick="myGeeks()"> 
        Click 
    </button> 
    <h4> 
            <p id="Geek_p" style="color:green"></p> 
        </h4> 
    <script> 
        function myGeeks() { 
            // access ColumnGroup element 
            var x = document.getElementById("myColGroup").id; 
            document.getElementById("Geek_p").innerHTML = x; 
        } 
    </script> 
</body> 
  
</html>

输出:

  • 在单击按钮之前:
  • 单击按钮后:

示例-2:使用document.getElementById(“myColGroup”).span从“colgroup id”返回span元素的数目;

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML | DOM ColumnGroup Object 
    </title> 
</head> 
<style> 
    #myColGroup { 
        background:green; 
    } 
      
    table { 
        color:white; 
        margin-left:180px; 
        background:yellow; 
    } 
      
    #Geek_p { 
        color:green; 
        font-size:30px; 
    } 
      
    td { 
        padding:10px; 
    } 
</style> 
  
<body style="text-align:center;"> 
  
    <h1 style="color:green;">   
            GeeksForGeeks   
        </h1> 
  
    <h2>HTML | DOM ColumnGroup Object</h2> 
    <table> 
        <colgroup id="myColGroup" span="2"> 
        </colgroup> 
        <tr> 
            <th>S.No</th> 
            <th>Title</th> 
            <th>Geek_id</th> 
        </tr> 
        <tr> 
            <td>Geek_1</td> 
            <td>GeekForGeeks</td> 
            <th>Geek_id_1</th> 
        </tr> 
        <tr> 
            <td>Geek_2</td> 
            <td>GeeksForGeeks</td> 
            <th>Geek_id_2</th> 
        </tr> 
    </table> 
    <br> 
    <button onclick="myGeeks()"> 
        Click 
    </button> 
    <h4> 
            <p id="Geek_p" style="color:green"></p> 
        </h4> 
    <script> 
        function myGeeks() { 
            // access ColumnGroup element 
            var x = document.getElementById("myColGroup").span; 
            document.getElementById("Geek_p").innerHTML = x; 
        } 
    </script> 
</body> 
  
</html>

输出

  • 在单击按钮之前:
  • 单击按钮后:

支持的浏览器:

  • 谷歌浏览器
  • 火狐浏览器
  • Edge
  • Safari
  • Opera

相关用法


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