开发者

geting the html of tables with greater than 4 rows stored in an array map(function()

With 开发者_StackOverflowthe following code in jquery:

  $(source).children('table').filter(function() {
return ($('tbody > tr', 'table').length >= 4)})

I filter out tables that have 4 rows or greater. I'm trying to create an array of the html from this using the map (function() but this doesn't seem to work. Im trying to get the html() of each table that satisfies this condition.


I think this is what you're looking for:

$(source).children("table").map(function() {
    if ($("tbody > tr", this).length >= 4) {
        return $(this).html();
    } else {
        return null;
    }
}).get();

Selector context in jQuery expects a DOM element, jQuery, or document. Using this will restrict the query to the element being evaluated in the filter function.


var htmlArray = $('#source').children('table')
                            .filter(function() {
                                return ($('tbody > tr', $(this)).length >= 4);
                          }).map(function(i, item) {
                                return $(item).html();
                          }).get();


You can do it all in the map()[docs] method.

var arr = $(source).children('table').map(function() {
    if( $('tbody > tr', this).length >= 4 ) return $(this).html();
}).get();

Or using the jQuery.map()[docs] method.

var arr = $.map($(source).children('table'), function(val,i) {
    if( $('tbody > tr', val).length >= 4 ) return $(this).html();
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜