English 中文(简体)
jj 查询表格是否有两条条件连续排列超过2 td 的情况
原标题:jQuery if table has more than 2 td in a row with 2 conditions

我试图合并两个条件 在一个“如果”声明,如果可能的话。

我想首先检查屏幕宽度, 然后看看表格的行或第一行中是否有超过2 tds。 如果有某种函数的话 。

这是我在宽度方面的第一个部分,但我在需要满足的其他条件中又应如何补充呢?

var width = parseInt($(window).width());
if (width<=610) {  
//Need to add the other condition that is a table has more than 2 td s in a row

}

这是我想运行的真实代码。 请看我的评论, 如果执行超过两列的话 。

var width = parseInt($(window).width());
if (width<=610) {   

    //Need to figure out how execute this if there are more than 2 columns in the table???
    $( .content table ).replaceWith(function() {

    var $th = $(this).find( th ); // get headers
    var th = $th.map(function() {
        return $(this).text();
    }).get(); // and their values

    $th.closest( tr ).remove(); // then remove them

    var $d = $( <div> , {  class :  responsive-table  });

    $( tr , this).each(function(i, el) {
        var $div = $( <div> , { class :  box grey-bg }).appendTo($d);
        $( td , this).each(function(j, el) {
            var n = j + 1;
            var $row = $( <div> , {
                // class :  row-  + n
                 class :  row 
            });
            $row.append(
            $( <span> , {
                // class :  label-  + n,
                 class :  label ,
                text: th[j]
            }),   :  , $( <span> , {
                // class :  data-  + n,
                 class :  data ,
                text: $(this).text()
            })).appendTo($div);
        });
    });
    return $d;  
    }); 

};
问题回答

试一下下面,假设你只是想得到一张桌子的数...

var width = parseInt($(window).width());
var tableColCount = $( #tableID tr:first ).find( td ).length;

if (width<=610 && tableColCount  > 2) {  
  //Your code

}
function checkResult() {
    $("#IDOfYourTable tr").each(function() {
        if ((($this).find("td").size() > 1) || (($this).find("th").size() > 1))
            return true;
    });
    return false;
}

3⁄4 ̄ ̧漯B

校正代码 :

var width = parseInt($(window).width());
var that = undefined;
function checkResult() {
    if (width <= 610) {
        $(".content table tr").each(function() {
            if (($(this).find("td").size() > 2) || ($(this).find("th").size() > 2))
            {
                if ((that == undefined) || (that[0] != this)) {
                that = $(this);
    var $th = $(this).find( th ); // get headers
    var th = $th.map(function() {
        return $(this).text();
    }).get(); // and their values

    $th.closest( tr ).remove(); // then remove them

    var $d = $( <div> , {  class :  responsive-table  });

    $( tr , this).each(function(i, el) {
        var $div = $( <div> , { class :  box grey-bg }).appendTo($d);
        $( td , this).each(function(j, el) {
            var n = j + 1;
            var $row = $( <div> , {
                // class :  row-  + n
                 class :  row 
            });
            $row.append(
            $( <span> , {
                // class :  label-  + n,
                 class :  label ,
                text: th[j]
            }),   :  , $( <span> , {
                // class :  data-  + n,
                 class :  data ,
                text: $(this).text()
            })).appendTo($div);
        });
    });
            }
        }
                                    });
    }
}
checkResult();                                    





​
//Need to figure out how execute this if there are more than 2 columns in the table???
function foo(that) {
    that.find("tr").each(function(){
        if ($(this).find("td").size() > 2)
            that.addClass("strip-these");  
            return;
    });
}

$( .content table ).each(function() {
    foo($(this));
});
console.log($(".strip-these"));
$( .strip-these ).each(function() {
    $( tr , this).each(function() {
        var length = $( td , this).length;
        console.log(length);
        if (length > 2) {
            // code
            $( .strip-these ).replaceWith(function() {

                var $th = $(this).find( th ); // get headers
                var th = $th.map(function() {
                    return $(this).text();
                }).get(); // and their values
                $th.closest( tr ).remove(); // then remove them
                var $d = $( <div> , {
                     class :  responsive-table 
                });

                $( tr , this).each(function(i, el) {
                    var $div = $( <div> , {
                         class :  box grey-bg 
                    }).appendTo($d);
                    $( td , this).each(function(j, el) {
                        var n = j + 1;
                        var $row = $( <div> , {
                            // class :  row-  + n
                             class :  row 
                        });
                        $row.append(
                        $( <span> , {
                            // class :  label-  + n,
                             class :  label ,
                            text: th[j]
                        }),   :  , $( <span> , {
                            // class :  data-  + n,
                             class :  data ,
                            text: $(this).text()
                        })).appendTo($div);
                    });
                });
                return $d;
            });



        }
    });
});​




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签