Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅当表格中的“HTML”数量超过X时,才将其添加到表格中;tds";使用JavaScript_Javascript_Jquery_Html - Fatal编程技术网

仅当表格中的“HTML”数量超过X时,才将其添加到表格中;tds";使用JavaScript

仅当表格中的“HTML”数量超过X时,才将其添加到表格中;tds";使用JavaScript,javascript,jquery,html,Javascript,Jquery,Html,如果找到了3个以上的td元素,我会尝试向表中添加一个HTML块,如果表中的td元素少于3个,则不会执行任何操作。我已经遍历了这些表,并且能够检查每个表中有多少tds 我已尝试使用.filter();和$.each(),但无法破解它 因此:>>如何只将HTML添加到传递if>3语句的表中 我还在学习JavaScript和jQuery,所以请耐心听我说 以下是迄今为止的代码: var tbls = $("body").find("table.responsive").length; var d

如果找到了3个以上的td元素,我会尝试向表中添加一个HTML块,如果表中的td元素少于3个,则不会执行任何操作。我已经遍历了这些表,并且能够检查每个表中有多少tds

我已尝试使用.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;          
     } 
});​
var tbls=$(“body”).find(“table.responsive”).length;
var data=Array();
对于(var index=0;index3){
//这里我想添加HTML,但它应用于所有表
$(“表.响应”)。在(“”)之前;
返回false;
} 
});​

这里是一个演示:

您不需要从body元素开始遍历并为此创建一个数组,您可以在每个函数中计算TDs

$("table.responsive").​each(function(){
    if ( $('td', this).length > 3) {
       $(this).before("<div style='position:relative;'><span class='MobiScroll'></span></div>");
    }
})​
$(“table.responsive”)。​每个(函数(){
如果($('td',this).length>3){
$(本)。在(“”)之前;
}
})​
$(“table.responsive”)。每个(函数(){
if($(this).find('td').size()>3)
$(本)。在(“”)之前;
})

$(函数(){
$(“table.responsive”)。每个(函数(){
if($(this.find('td')。长度>3){
$(本)。在(“”)之前;
//警报($(此))
}
})});

所有人类行为都有以下七种原因中的一种或多种:机会、天性、强迫、习惯、理性、激情、欲望。“亚里士多德”哇谢谢你的快速回复,效果很好。我担心事情会这么简单@ClydeLobo原因仍然未知。我印象深刻,在讨论JavaScript或html时,你很少看到亚里士多德式的引用…=)@大卫托马斯:太好了,我喜欢尼采的作品和存在主义我想我需要更多地学习“这个”。谢谢大家的回复。谢谢大家花时间,这看起来也很棒:)
$("table.responsive").each(function(){
  if($(this).find('td').size() > 3)
    $(this).before("<div style='position:relative;'><span class='MobiScroll'></span></div>");
})
$(function(){
    $("table.responsive").each(function(){
    if ( $(this).find('td').length > 3) {
       $(this).before("<div style='position:relative;'><span class='MobiScroll'></span></div>");
        //alert($(this))
    }
})});