如何获取 jquery 变量 childeren

How to get jquery variable childeren?

本文关键字:jquery 变量 childeren 获取 何获取      更新时间:2024-02-23

嗨,我有一个接受id作为函数参数的方法:

function exportDetails($div) {
    $($div).each(function () {
        alert(this.id);
});

我需要获取此div内的所有表id,类似于以下内容:

function exportDetails($div) {
    $($div > table).each(function () {
        alert(this.id);
});

以下是我调用函数的方式,每个 itemDetails 都有动态生成的表,我需要获取它们的 id,因为它们都是唯一的:

exportDetails.apply(this, [$('#itemDetails')]);

谢谢!

这样的事情应该可以工作:

function exportDetails($div) {
    return [].map.call($div.children('table'), function(table) {
        return table.id;
    });
}

即只需返回一个数组,其中包含作为 $div 的直接后代找到的每个table元素的.id

您的代码格式不正确。另外,我不知道您是否正在向该函数提供一组位于$div参数内的表。以下是一些选项。

// this works if you pass in an element with nested table that have ids. 
function exportTableToCSV($div) {
    $($div+ ' > table').each(function () {
        alert( $(this).attr('id') );
    });
}
// this works if you want to get all the HTML elements with an id or if you want to get all the ids of elements which also have the same class assigned to them.
function exportTableToCSV($div) {
    $($div).each(function () {
        alert( $(this).attr('id') );
    });
}
exportTableToCSV('div');
// or
exportTableToCSV('.yourClass');