Javascript:查找所有“输入”在表中

发布于 2024-12-03 15:48:36 字数 532 浏览 0 评论 0原文

有没有更短的方法在 JavaScript 中编写这个?

    var data = [];
    var table = document.getElementById( 'address' );
    var rows = table.getElementsByTagName( 'tr' );
    for ( var x = 0; x < rows.length; x++ ) {
        var td = rows[x].getElementsByTagName( 'td' );
        for ( var y = 0; y < td.length; y++ ) {
            var input = td[y].getElementsByTagName( 'input' );
            for ( var z = 0; z < input.length; z++ ) {
                data.push( input[z].id );
            }
        }
    }

Is there a shorter way to write this in JavaScript?

    var data = [];
    var table = document.getElementById( 'address' );
    var rows = table.getElementsByTagName( 'tr' );
    for ( var x = 0; x < rows.length; x++ ) {
        var td = rows[x].getElementsByTagName( 'td' );
        for ( var y = 0; y < td.length; y++ ) {
            var input = td[y].getElementsByTagName( 'input' );
            for ( var z = 0; z < input.length; z++ ) {
                data.push( input[z].id );
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

远山浅 2024-12-10 15:48:36

element.getElementsByTagName< /a> 查找所有后代,而不仅仅是子代,因此:

<script type="text/javascript> 

    var data = []; 
    var table = document.getElementById( 'address' ); 
    var input = table.getElementsByTagName( 'input' ); 
    for ( var z = 0; z < input.length; z++ ) { 
        data.push( input[z].id ); 
    } 

</script> 

element.getElementsByTagName finds all descendants, not only children, so:

<script type="text/javascript> 

    var data = []; 
    var table = document.getElementById( 'address' ); 
    var input = table.getElementsByTagName( 'input' ); 
    for ( var z = 0; z < input.length; z++ ) { 
        data.push( input[z].id ); 
    } 

</script> 
十六岁半 2024-12-10 15:48:36

是的。

var data = [],
    inputs = document.getElementById('address').getElementsByTagName('input');

for (var z=0; z < inputs.length; z++)
  data.push(inputs[z].id);

请注意,即使在具有三个循环的较长版本中,您也可以说:

var rows = table.rows;
// instead of
var rows = table.getElementsByTagName('tr');

// and, noting that it returns <th> cells as well as <td> cells,
// which in many cases doesn't matter:
var tds = rows[x].cells;
// instead of
var tds = rows[x].getElementsByTagName('td');

Yep.

var data = [],
    inputs = document.getElementById('address').getElementsByTagName('input');

for (var z=0; z < inputs.length; z++)
  data.push(inputs[z].id);

Note, even in your longer version with three loops you can also say:

var rows = table.rows;
// instead of
var rows = table.getElementsByTagName('tr');

// and, noting that it returns <th> cells as well as <td> cells,
// which in many cases doesn't matter:
var tds = rows[x].cells;
// instead of
var tds = rows[x].getElementsByTagName('td');
第七度阳光i 2024-12-10 15:48:36

对于现代浏览器:)

var table, inputs, arr;

table = document.getElementById( 'test' );
inputs = table.querySelectorAll( 'input' );
arr = [].slice.call( inputs ).map(function ( node ) { return node.id; });

现场演示: http://jsfiddle. net/HHaGg/

因此,我使用了 map 方法,而不是 for 循环 - 每个数组元素(每个 INPUT 节点)都替换为其 ID价值。

另请注意,`inputs.map(... 不起作用,因为 inputs 是一个 NodeList 元素 - 它是一个类似数组的对象,但不是标准数组。要仍然使用 map 方法,我们只需将其转换为一个数组,这就是 [].slice.call(inputs )对我们来说。

For modern browsers :)

var table, inputs, arr;

table = document.getElementById( 'test' );
inputs = table.querySelectorAll( 'input' );
arr = [].slice.call( inputs ).map(function ( node ) { return node.id; });

Live demo: http://jsfiddle.net/HHaGg/

So instead of a for loop, I make use of the map method - each array element (each INPUT node) is replaced with its ID value.

Also note that `inputs.map(... doesn't work since inputs is a NodeList element - it's an array-like object, but not an standard array. To still use the map method on it, we just have to transform it into an array which is what [].slice.call( inputs ) does for us.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文