Rails3:jquery正在使用AJAX切换视图,如何获得链接的焦点状态?
刚刚用AJAX更改了我的视图,但是现在我被难住了,如何使请求的操作具有:focus-type css选项,这样当用户选择“About”和“About-partial-load”时,我就可以使用带有样式的“About”链接来显示About页面。非常感谢 在单击的链接上设置一个类,从所有其他链接中删除该类。例如:Rails3:jquery正在使用AJAX切换视图,如何获得链接的焦点状态?,jquery,ruby-on-rails,css,ruby-on-rails-3,Jquery,Ruby On Rails,Css,Ruby On Rails 3,刚刚用AJAX更改了我的视图,但是现在我被难住了,如何使请求的操作具有:focus-type css选项,这样当用户选择“About”和“About-partial-load”时,我就可以使用带有样式的“About”链接来显示About页面。非常感谢 在单击的链接上设置一个类,从所有其他链接中删除该类。例如: $('a.tab_link').click(function(){ $('a.tab_link').removeClass('active') $(this).addClass('
$('a.tab_link').click(function(){
$('a.tab_link').removeClass('active')
$(this).addClass('active')
})
// CSS
a.tab_link.active {
color: red;
}
在应用程序中可以有这样的帮助程序。\u helper.rb
def nav_link(name, url)
selected = url.all? { |key, value| params[key] == value }
link_to(name, url, :class => (selected ? "youarehere tabs" : "tabs"))
end
那么在你看来你可以
<div id="menu-header">
<div class="subheader answers-subheader">
<div id="tabs" class="tabs2">
<%= nav_link "example", :view => nil %>
<%= nav_link "example1", :view => "example1"%>
<%= nav_link "example2", :view => "example2" %>
</div>
</div>
</div>
<div id="menu-items">
<%= %w[example example1 example2 ].include?(params[:view]) ? render(params[:view]) : render("example") %>
</div>
零%>
“示例1”%>
“示例2”%>
然后在application.js中
$(function() {
$(".tabs2 .tabs").click(function(e) {
$("#tabs .tabs").removeClass("youarehere");
$(this).addClass("youarehere");
$("#menu-items").append('<div class="progress"><img src="/assets/loading.gif" width="32" height="32" alt="" /></div>');
$.getScript(this.href);
if (history && history.replaceState) {
history.replaceState(null, document.title, this.href);
}
e.preventDefault();
});
});
$(函数(){
$(“.tabs2.tabs”)。单击(函数(e){
$(“#tabs.tabs”).removeClass(“youarehere”);
$(this.addClass(“youarehere”);
$(“#菜单项”)。附加(“”);
$.getScript(this.href);
if(history&&history.replaceState){
replaceState(null,document.title,this.href);
}
e、 预防默认值();
});
});
我希望这有助于看起来不错!所以这只适用于我添加class=“tab\u link”到的链接?美元(这个)指的是什么?谢谢
$(this)
返回所单击链接的JQuery对象。您好,我似乎无法使其正常工作。你能给我一个JSFIDLE看看吗?谢谢。