2017-01-17 45 views
4

在引导,当你做如下崩塌效应:为什么bootstrap都需要类名和Id名称来折叠?

<p> 
    <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample"> 
    Link with href 
    </a> 
    <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample"> 
    Button with data-target 
    </button> 
</p> 
<div class="collapse" id="collapseExample"> 
    <div class="card card-block"> 
    Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. 
    </div> 
</div> 

为什么按钮引用类data-toggle=collapse及ID面板效果的data-target=#collapseExample?没有足够的ID来切换其状态?

是因为数据切换指的是它将要使用哪种效果因此将面板与它一起定位?

回答

3

data-toggledata-target属性都被使用,因为data-toggle也被其他Bootstrap组件使用(即:崩溃,模式,选项卡,下拉等等)。

通过指定data-toggle="collapse"您告诉Bootstrap API将折叠行为应用于由data-target指定的元素。如果您只指定data-target,则API无法了解要使用哪个Component

+0

非常感谢!它让我感到困惑,因为类名也被命名为'collapse',我最终认为它也是针对类名的。 – Chopnut