【问题标题】:Is there a Twitter Bootstrap class that means "initially hidden"?是否存在意味着“最初隐藏”的 Twitter Bootstrap 类?
【发布时间】:2014-10-14 14:24:52
【问题描述】:

使用 Bootstrap 3,我在页面上有一个元素,我想稍后显示以响应用户单击按钮。示例:

<div id="search_results">
... gets populated from ajax data later ...
</div>

<button id="search_button" type="button" class="btn btn-primary pull-right">Search</button>

<script>
$('#search_button').click(function() {
    // ... do the call to search
    // and in the callback: 
    $('#search_results').show();
});
</script>

search_results div 最初应该是隐藏的。是否有一些正常/最佳实践方法可以使用引导程序执行此操作?

是的,我确实意识到我可以将 style="display:none" 放在 search_results 上,但这是最好的方法吗?拥有一种语义上意味着“最初隐藏”的样式似乎会更好一些。 (注意:隐藏或隐藏类不会这样做,因为它们是 !important 并且 show()、toggle() 等使用不会覆盖它们的内联样式,即设置“隐藏”作为类使其无法显示来自 jQuery。)

【问题讨论】:

标签: jquery twitter-bootstrap


【解决方案1】:

您可以使用hidden 类来隐藏元素

<div id="search_results" class="hidden">
... gets populated from ajax data later ...
</div>

然后

$('#search_button').click(function () {
    // ... do the call to search
    // and in the callback: 
    $('#search_results').removeClass('hidden');
});

演示:Fiddle

Showing and hiding content

【讨论】:

  • 这不起作用,因为 hidden 有 !important (在链接中也有说明)。并且 jQuery 的 show() 设置了内联样式,但不设置 !important,这不太具体。 IE。它不起作用。
  • 对 - 当然可以通过删除类来使其工作 - 我看到你的编辑。只是想知道是否有一个类实际上意味着它最初是隐藏的,然后可以被另一个内联样式覆盖。看起来不像。如果没有人提出更好的答案,我会接受。
  • @bgp 我不这么认为......当然你可以自己写一个吗? :)
  • 哦,你知道这些样式是怎么回事,这么多额外的击键... ;)
【解决方案2】:

查看源代码表明.collapse.invisible 是选项,具体取决于您是想要display: none; 行为还是visibility: hidden; 行为。

2016 年 3 月 8 日更新:Bootstrap v4 的早期采用者应该注意.invisible 的规则现在是visibility: hidden !important;。 (.collapse 不变。)

【讨论】:

  • 不错! “折叠” - 非常有意义,与“最初隐藏”的概念非常相似。 (而且有效)
猜你喜欢
  • 2011-09-02
  • 1970-01-01
  • 2014-01-23
  • 2018-06-14
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 2019-12-13
相关资源
最近更新 更多