简体   繁体   English

如何使用javascript给特定的td一个类名?

[英]how to give a certain td a class name with javascript?

In a foreach loop in php, a list of rows with its td is created. 在php中的foreach循环中,将创建带有td的行列表。 In 1 of these rows, the folder has the name tmp . 在这些行的1中,该文件夹的名称为tmp Is it possible to give that td a class hidden so that the folder is not visible? 是否可以给该td一个hidden的类,以便该文件夹不可见?

This is how the foreach produces my html: 这是foreach产生我的html的方式:

<tr>
  <td>
     <a href="#">tmp</a>
  </td>
  <td>
     Jan 31 '19
  </td>
  <td>
     1.2 Kb
  </td>
  <td>
     .png
  </td>
  ///

So: the td in which the anchor with the name tmp should get the class name: hidden liek below: 因此: td ,其中名称为tmp的锚应获得类名称:下图所示为hidden

<tr>
  <td class="hidden">
     <a href="#">tmp</a>
  </td>
  <td>
     Jan 31 '19
  </td>
  <td>
     1.2 Kb
  </td>
  <td>
    .png
  </td>
  ///

Why not just give it the class on the server? 为什么不给它服务器上的类呢?

If not then 如果没有

 //$("td:has('a')").addClass("hidden"); // OR $("td:has('a:contains(tmp)')").hide() 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td> <a href="#">tmp</a> </td> <td> Jan 31 '19 </td> <td> 1.2 Kb </td> <td> .png </td> </tr> </table> 

I wonder why you are not doing it on server side as @mplungjan suggested, but if you want to do it on client side instead of server side; 我想知道为什么您不按照@mplungjan的建议在服务器端执行此操作,但是如果要在客户端而不是服务器端执行此操作;

 $(function() { $("#folders a:contains('tmp')").parents("td").addClass("hidden"); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="folders"> <tr> <td> <a href="#">tmp</a> </td> <td> Jan 31 '19 </td> <td> 1.2 Kb </td> <td> .png </td> </tr> </table> 

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

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