【问题标题】:Change body tag on page load for bootstrap 2.0在引导 2.0 的页面加载时更改正文标记
【发布时间】:2026-01-14 20:00:01
【问题描述】:

我正在使用引导程序,并且我有一个带有引导程序的 scrollspy 页面。我无法将 scrollspy 添加到其他页面,因为我的链接意外触发。是否可以使用简单的jQuery函数来添加:

data-spy="scroll"data-target=".subnav" 在特定页面加载时添加到页面正文。

将滚动间谍添加到div="span12" 似乎不起作用,但是,当放置在主体上时,该功能确实有效。

如果其他人有使用此方法或其他方法的简单解决方案,请告诉我。

这是我目前的代码:

<body> <!-- Add it here to make it work -->
 <div class="span12" style="background: #fff;"> <!-- OR add it here and see if it works, if so then I can avoid the function all together. -->
  <div class="span11">
  <header>
     <h1>TITLE</h1>
     <h3>The full history of..<small>By Jane Doe</small></h3>
     <div class="subnav">
       <ul class="nav nav-pills">
       <li class="active"><a href="#overview">Introduction</a></li>
       ...
       </ul>
  </div>
  </header>

  <section id="overview"></section>
  <section id....
 </div>
</div>

还有我用来修复 topnav 的脚本:

$(document).scroll(function(){
    // If has not activated (has no attribute "data-top"
if (!$('.subnav').attr('data-top')) {
    // If already fixed, then do nothing
    if ($('.subnav').hasClass('subnav-fixed')) return;
    // Remember top position
    var offset = $('.subnav').offset()
    $('.subnav').attr('data-top', offset.top);
}

if ($('.subnav').attr('data-top') - $('.subnav').outerHeight() <= $(this).scrollTop())
    $('.subnav').addClass('subnav-fixed');
else
    $('.subnav').removeClass('subnav-fixed');
});

$('#navbar').scrollspy();

【问题讨论】:

    标签: jquery html css twitter-bootstrap


    【解决方案1】:

    试试

    $(document).load(function() {
        $('body').attr('data-spy', 'scroll').attr('data-target', '.subnav');
    });
    

    【讨论】:

    • 此外,引导文档页面具有数据目标子导航。我需要指定才能工作吗?