繁体   English   中英

单击时如何在链接中添加和删除类名?

[英]How to add and remove a class name from a link when clicked?

我想在Rails应用程序的表格中切换2个链接( send_emailemail_sent )的可见性。 两个链接都在同一个单元中。

<table >
<% @applications.each do |application| %>
 <tr>
  <td>
   <a href="mailto:grant@example.com" class="send_email">Invite for an interview</a>

   <a href="" class="email_sent hidden">Undo</a>
  </td>
 </tr>
<% end %>
</table>

在我的CSS中

.hidden{display: none;}

这是我的javascript

<%= javascript_tag do %>
 $(function(){
  $('.send_email > a').click(function(){
   // add the hidden class to send_email
   // remove the hidden class from the next email_sent link
  });
  $('.email_sent > a').click(function(){
   // add the hidden class to email_sent
   // remove the hidden class from the previous sent_email link
  });
 });
<% end %>

您可以简单地使用jQuery中的removeClass()addClass()

删除类文档。

添加类文档。

你做错了。 $('.send_email > a')表示您具有send_email类到锚点的父元素。 $('a.send_email')是选择带有类别的锚点的正确方法。

我会使用hide()show()这样的:

$('a.send_email').click(function(){
  $(this).hide();
  $('.email_sent').show();
});

$('a.email_sent').click(function(){
  $(this).hide();
  $('.send_email').show();
});

暂无
暂无

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

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