[英]How to add and remove a class name from a link when clicked?
我想在Rails應用程序的表格中切換2個鏈接( send_email
和email_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 %>
你做錯了。 $('.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.