Ruby on Rails 또는 다른 서버 측 스크립트를 사용하는 경우 anchor경로에 옵션 을 사용하는 것이 좋습니다. 페이지가로드되면 URL 해시를 사용할 수 없기 때문입니다. 링크 또는 양식 제출을 통해 올바른 탭을 제공 할 수 있습니다.
<%= form_for @foo, url: foo_path(@foo, anchor: dom_id(foo)) do |f| %>
# Or
<%= link_to 'Foo', foo_path(@foo, anchor: dom_id(foo)) %>
창이 ID로 점프하는 것을 방지하기 위해 접두사를 사용하는 경우 :
<%= form_for @foo, url: foo_path(@foo, anchor: "bar_#{dom_id(foo)}") do |f| %>
그렇다면 CoffeeScript :
hash = document.location.hash
prefix = 'bar_'
$('.nav-tabs a[href=' + hash.replace(prefix, '') + ']').tab 'show' if hash
$('.nav-tabs a').on 'shown.bs.tab', (e) ->
window.location.hash = e.target.hash.replace '#', '#' + prefix
또는 JavaScript :
var hash, prefix;
hash = document.location.hash;
prefix = 'bar_';
if (hash) {
$('.nav-tabs a[href=' + hash.replace(prefix, '') + ']').tab('show');
}
$('.nav-tabs a').on('shown.bs.tab', function(e) {
window.location.hash = e.target.hash.replace('#', '#' + prefix);
});
이것은 Bootstrap 3에서 작동합니다.