繁体   English   中英

带有if语句的表的最后td-jQuery

[英]last td of table with if statement - jquery

关于这个问题有很多答案,但是没有一个包含任何if语句。 所以,我需要这样的东西:

if($(this) == "last <td> of the row")

我怎样才能做到这一点? 谢谢

您可以使用:last-child选择器来测试其选择器是否是其父母的最后一个孩子。

从jQuery API文档中:

:最后一个孩子

  • 选择器选择作为其父级的最后一个子级的所有元素。
  • :last仅匹配一个元素,而:last-child可以匹配多个:每个父元素匹配一个。

因此,如果您使用td:last ,则仅当在代码段中单击8时才会变为true(如果您不使用td:last而是:last ,则td不会测试为true,因为它们都不是last元素在DOM中)

如果您使用:last-child ,则单击48都将测试为true,因为它们都是tr中的最后一个td

 $('td').click(function() { console.log($(this).text()); if ($(this).is(':last-child')) { console.log('last'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <table> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> </tr> </table> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM