为什么我不能以编程方式为 TH 标签设置 colspan?



我有一个带有 thead 的表,第一个 colspan = 1。我想以编程方式将其设置为值 2:它不起作用,如何做到这一点?

这是源代码:

https://jsfiddle.net/Lgof8m6q/

<table>
    <thead>
        <tr>
        <th scope="col" colspan="1">TITLE</th>
        </tr>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>    

        </tr>
    </tbody>
</table>

JavaScript :

    var myTable = document.getElementsByTagName('table')[0];
    var myThead = myTable.getElementsByTagName('thead')[0]; 
    _tr = myThead.getElementsByTagName("TR")[0];
    _th = _tr.getElementsByTagName("TH")[0]; 
    _th.colspan = 2;

这是colSpan,而不是colspan ;-)

请参阅 colSpan 文档。

(更新的小提琴)。

Javascript DOM元素有一个特殊的方法来设置属性,这是setAttribute('<attribute>','<value>')

_th.setAttribute("colspan", "2");

检查这个小提琴:

https://jsfiddle.net/Lgof8m6q/7/

最新更新