2012-09-28 19 views
2

如果找到超过3个td元素,并且不执行任何操作,则尝试将HTML块添加到表格中表中的元素少于3个。我已经循环遍历表格,并且能够检查每个表格中有多少个tds。仅当HTML中的HTML数量超过X时才将HTML添加到表格

我试过使用.filter();和$ .each(),但无法破解它。

因此:>>如何将HTML添加到传递if> 3语句的表中?

我仍在学习JavaScript和jQuery,请耐心等待。

这里是迄今为止代码:

var tbls = $("body").find("table.responsive").length; 
var data = Array(); 

for(var index = 0; index < tbls; index++){ 
    $("table.responsive").each(function(i, v){ 
     data[i] = Array(); 
     $(this).find("td").each(function(ii, vv){ 
      data[i][ii] = $(this).text(); 
    }); 
    }) 
} 

data.forEach(function(item) { 
    if(item.length > 3) { 

     //here I want to add the HTML, but it is applied to all tables 
     $("table.responsive").before("<div style='position:relative;'><span class='MobiScroll'></span></div>"); 
    return false;   
    } 
});​ 

这里是一个演示:http://jsfiddle.net/design4lifeblog/gSPXu/15/

+0

我想我需要学习 “这个” 了。谢谢大家的回复。 – Tony

回答

1

你并不需要开始从身体穿越元素,使这个数组,可以算每个功能中的TD。

$("table.responsive").​each(function(){ 
    if ($('td', this).length > 3) { 
     $(this).before("<div style='position:relative;'><span class='MobiScroll'></span></div>"); 
    } 
})​ 
+1

_所有的人类行为都有以下七个原因中的一个或多个:机会,自然,强迫,习惯,理性,激情,欲望。“Aristotle” – undefined

+0

downvoter可以解释downvote的原因吗? –

+0

谢谢你的迅速回复,很好。我担心这会很简单! – Tony

0
$("table.responsive").each(function(){ 
    if($(this).find('td').size() > 3) 
    $(this).before("<div style='position:relative;'><span class='MobiScroll'></span></div>"); 
}) 

http://jsfiddle.net/pKWXZ/1/

1
$(function(){ 
    $("table.responsive").each(function(){ 
    if ($(this).find('td').length > 3) { 
     $(this).before("<div style='position:relative;'><span class='MobiScroll'></span></div>"); 
     //alert($(this)) 
    } 
})}); 

DEMO

+0

感谢您抽出时间,这似乎也很棒:) – Tony