[英]Change the css when tbody doesn't have a row using JQUERY
I want to change the css
through jquery
if table body doesn't have a row. 如果表主体没有行,我想通过
jquery
更改css
。 How can i achieve this? 我怎样才能做到这一点? Here's my approach.
这是我的方法。 My problem is it doesn't work, I tried to put an
alert
inside the if
statement but alert
doesn't show up. 我的问题是它不起作用,我试图在
if
语句中放置一个alert
,但是alert
没有显示。 I want to hide the table
if tbody
doesn't have a tr
. 如果
tbody
没有tr
我想隐藏table
。
$(document).ready(function() { var tbody = $(".table-condensed tbody"); if (tbody.children().length == 0) { $(".blankdata").css("display", "none"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table table-bordered table-condensed"> <thead> <tr> <th class="text-center blankdata">EMPLOYEE NAME</th> <th class="text-center blankdata">DESCRIPTION</th> <th class="text-center blankdata"># of Payments</th> <th class="text-center blankdata">LOAN AMOUNT</th> <th class="text-center blankdata">TOTAL PAYMENT</th> <th class="text-center blankdata">BALANCE</th> <th></th> </tr> </thead> <tbody class="table-warehouse"> <tr id="blank"> <td class="text-center blankdata"></td> <td class="text-center blankdata"></td> <td class="text-center blankdata"></td> <td class="text-center blankdata"></td> <td class="text-center blankdata"></td> <td class="text-center blankdata"></td> <td class="text-center"></td> </tr> </tbody> </table>
尝试:
if (!$('.table-condensed tbody tr').length)
Your code is right. 您的代码是正确的。 All you have to do is change the condition to
!==
temporarily to check your css property. 您要做的就是暂时将条件更改为
!==
来检查css属性。
Since you have a tr
inside, the if condition is failing. 由于您内部有
tr
,因此if条件失败。 If you remove your tr
, your if condition will run fine. 如果删除
tr
,则if条件将运行良好。 In this case you can temporarily change it to !==
to check that. 在这种情况下,您可以将其临时更改为
!==
进行检查。
$(document).ready(function() { var tbody = $(".table-condensed tbody"); if (tbody.children().length !== 0) { $(".blankdata").css("display", "none"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table table-bordered table-condensed"> <thead> <tr> <th class="text-center blankdata">EMPLOYEE NAME</th> <th class="text-center blankdata">DESCRIPTION</th> <th class="text-center blankdata"># of Payments</th> <th class="text-center blankdata">LOAN AMOUNT</th> <th class="text-center blankdata">TOTAL PAYMENT</th> <th class="text-center blankdata">BALANCE</th> <th></th> </tr> </thead> <tbody class="table-warehouse"> <tr id="blank"> <td class="text-center blankdata"></td> <td class="text-center blankdata"></td> <td class="text-center blankdata"></td> <td class="text-center blankdata"></td> <td class="text-center blankdata"></td> <td class="text-center blankdata"></td> <td class="text-center"></td> </tr> </tbody> </table>
If you want to detect whether there is no <tr>
tag under <tbody>
, you can use find()
method of Jquery: 如果要检测
<tbody>
下是否没有<tr>
标记,可以使用Jquery的find()
方法:
$(document).ready(function() {
var tbody = $(".table-condensed tbody");
if (tbody.find("tr").length == 0) {
$(".blankdata").css("display", "none");
}
});
However, in your example there is a <tr>
element inside <tbody>
, so the length is not 0. 但是,在您的示例中,
<tbody>
有一个<tr>
元素,因此长度不为0。
In addition; 此外; If there is no tag, you can not change its css of course.
如果没有标签,则您当然不能更改其CSS。
You can do 你可以做
if($(".table-condensed tr").length==0))
$(".table-condensed tr").css("display", "none");
Your current markup dose not produce true
condition because you have tr
inside tbody
that means if (tbody.children().length == 0)
is false
. 您当前的标记不会产生
true
条件,因为您在tbody
内有tr
,这意味着if (tbody.children().length == 0)
为false
。 I have checked it is really empty tbody
it is working fine for me: 我已经检查了它确实是空
tbody
它是为我工作的罚款:
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th class="text-center blankdata">EMPLOYEE NAME</th>
<th class="text-center blankdata">DESCRIPTION</th>
<th class="text-center blankdata"># of Payments</th>
<th class="text-center blankdata">LOAN AMOUNT</th>
<th class="text-center blankdata">TOTAL PAYMENT</th>
<th class="text-center blankdata">BALANCE</th>
<th></th>
</tr>
</thead>
<tbody class="table-warehouse">
</tbody>
</table>
According to your HTML please find working code bellow 根据您的HTML,请在下面找到工作代码
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th class="text-center blankdata">EMPLOYEE NAME</th>
<th class="text-center blankdata">DESCRIPTION</th>
<th class="text-center blankdata"># of Payments</th>
<th class="text-center blankdata">LOAN AMOUNT</th>
<th class="text-center blankdata">TOTAL PAYMENT</th>
<th class="text-center blankdata">BALANCE</th>
<th></th>
</tr>
</thead>
<tbody class="table-warehouse">
<tr id="blank">
<td class="text-center blankdata"></td>
<td class="text-center blankdata"></td>
<td class="text-center blankdata"></td>
<td class="text-center blankdata"></td>
<td class="text-center blankdata"></td>
<td class="text-center blankdata"></td>
<td class="text-center"></td>
</tr>
</tbody>
</table>
</body>
<script type="text/javascript">
$(document).ready(function() {
var applyclass = true;
var tbody = $(".table-condensed tbody");
tbody.find('td').each(function() {
var st = $(this).html();
if($.trim(st) != ''){
applyclass = false;
}
}) ;
if(applyclass){
$(".blankdata").css("display", "none");
}
});
</script>
</html>
I hope it will help you. 希望对您有帮助。
Put this in footer under jquery link...It generates alerts and works fine !!! 把它放在jQuery链接下的页脚中...它会生成警报并正常工作!!!
<script type="text/javascript">
var rowCount = $('.table-condensed .table-warehouse tr').length;
if( rowCount == 0){
alert("no rows in tbody");
}else{
alert("tbody has rows");
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.