我有以下html(小提琴链接)
<div class="thumbnail_follow" id="follow_btn-2">
<a class="btn btn-primary" data-method="post" data-remote="true" rel="nofollow">Follow</a>
</div>
单击该按钮后,我希望该按钮将其自身替换为带有 html 的取消关注按钮
<div class="thumbnail_follow" id="follow_btn-2">
<a href="/users/2/unfollow" class="btn btn-danger" data-method="post" data-remote="true" rel="nofollow">Unfollow</a>
</div>
所以基本上,改变按钮和它指向的链接
我像这样使用 jQuery html()
$("#follow_btn-2").html('<a class=\"btn btn-danger\" data-method=\"post\" data-remote=\"true\" rel=\"nofollow\">Unfollow<\/a>');
请注意,这是通过代码中的 rails ujs 生成的
$("div[id='follow_btn-<%= @user.id %>']").html('<%=j link_to "Unfollow", unfollow_user_path(@user), :remote => true, :method => :post , :class=>"btn btn-danger"%>');
从jsfiddle可以看出,问题在于,一旦按钮被红色按钮替换,它就会跳开,就好像它的样式搞砸了一样。
我不明白是什么导致了跳转以及如何修复它。它是 css 问题还是 jQuery 使用不当?
这又是小提琴