如何获得jquery变量childeren?

时间:2016-04-07 22:09:31

标签: javascript jquery

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

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

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

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

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

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

谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码格式不正确。此外,我不知道你是否正在为这个函数提供一组在$ 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');