开发者

Process tables by number of rows, row height, and alert if no tables exist in jquery

I have th开发者_如何学Gois code right now:

var table = $(source).children('table').map(function() {
if (this.rows.length >= 4) {
    return $(this).outerHTML();
} else {
    return null;
}
}).get();

This will get tables by number of rows, but how would I get the tables by row height (if height < 100 px) also and also alert if no tables are in the array (if the array is empty).


You could use .is() method, like this:

var table = $(source).children('table').map(function() {
if ($(this).find("tr").is(function(){ $(this).height() < 100 }) {
    return $(this).outerHTML();
} else {
    return null;
}
}).get();
//Now check size:
if(table.length == 0) alert("There are no tables");

What are we doing with .is()? We are searching if at least one of the children tr has height lower than 100, and ignore the table if this condition is not fulfilled.

Hope this helps. Cheers

EDIT For your new demands:

  var table = $(source).children('table').map(function() {
    if (!$(this).find("tr").is(function(){ $(this).height() >= 100 } &&
         $(this).find("tr").size() > 4  ) {
        return $(this).outerHTML();
    } else {
        return null;
    }
  }).get();
  //Now check size:
  if(table.length == 0) alert("There are no tables");


Not sure what you mean by the first question, but for the alert:

if($(source).children('table').length===0){
   alert("no tables!");
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜