简体   繁体   English

javascript:更改其他div内的多个div的属性

[英]javascript: change attributes of multiple div's inside other div

I have table: 我有桌子:

<table class="table table-condensed" id="mbusTable">
    <label><h5>rtu</h5></label>
    <tbody>
        <td><input type="text" id="mid" class="span1" placeholder="mid"></td>
        <td><input type="text" id="type" class="span1" placeholder="typem"></td>
        <td><input type="text" id="inverce" class="span1" placeholder="inverce"></td>
        <td><input type="text" id="mbaddr" class="span1" placeholder="mbaddr"></td>
    </tbody> 
</table>

All I want is to add _mbusTable to all inputs inside it. 我想要的只是将_mbusTable添加到其中的所有输入。 Here's what I have now: 这就是我现在拥有的:

        function correctIDs(tableID)
            var table = document.getElementById(tableID);
            var colCount = table.rows[0].cells.length;
            var prefix = tableID;
            for(var i=0; i<colCount; i++) {
                prefix += "_" + table.childNodes[1].getAttribute('id');
                table.childNodes[1].setAttribute('id',prefix);
                prefix = tableID;
            }
        };

But I guess it changes only first <input> . 但我想它只会改变第一个<input> How can I jump to the next? 我怎么跳到下一个? Sorry if it's something obvious. 对不起,如果这是显而易见的事情。 Thanks 谢谢

function correctIDs(tableID) {
    var table = document.getElementById(tableID);
    var inputs = table.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        var prefix = tableID + "_" + inputs[i].getAttribute('id');
        inputs[i].setAttribute('id', prefix);
    }
};


correctIDs("mbusTable");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM