【问题标题】:How to show different text in a div using Bootstrap 3?如何使用 Bootstrap 3 在 div 中显示不同的文本?
【发布时间】:2014-11-08 05:45:05
【问题描述】:

所以我有一个包含 5 张不同照片的页面。当您单击每张照片时,下面的 div 将显示随附的文本,然后一旦您再次单击照片,文本将隐藏。我只是 javascript 的初学者,但我想要实现的是根据用户点击的照片显示不同的文本。

现在,当您单击照片时,会显示文字。但是,当您单击另一张照片时,新文本将显示在正下方。我要问的是,您如何更改 jquery 代码,以便在您单击照片时显示文本,但是当您单击另一张照片时,旧文本将隐藏而新文本将显示。我正在使用 Bootstrap 的所有 jQuery 'Collapse' 功能。

提前致谢!

我在这里有一个靴子:http://www.bootply.com/sabby_fields/h7iP24nwST

另外,如果可能的话,我将如何显示文字随附的照片?比如哪张照片是活跃的?就像一个简单的文本颜色变化。

这是 HTML 代码:

<div class="col-md-3"></div>
    <div class="col-md-9">
        <div class="team-bio">
            <div class="row">
                <div class="col-xs-3 col-sm-3 col-md-3">
                    <div data-toggle="collapse" data-parent="#accordion" href="#collapseOne" class="collapsed" aria-expanded="false">
                <img class="img-responsive img-team-bio" src="http://placehold.it/150" style="margin-bottom:10px;" contenteditable="false">
                 <h4 class="">Name 1</h4>

            </div>
        </div>
        <div class="col-xs-3 col-sm-3 col-md-3">
            <div data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" class="collapsed">
                <img class="img-responsive img-team-bio" src="http://placehold.it/150" style="margin-bottom:10px;">
                 <h4 class="">Name 2</h4>

            </div>
        </div>
        <div class="col-xs-3 col-sm-3 col-md-3">
            <img class="img-responsive img-team-bio" src="http://placehold.it/150" style="margin-bottom:10px;">
             <h4 class="">Name 3</h4>

        </div>
        <div class="col-xs-3 col-sm-3 col-md-3">
            <img class="img-responsive img-team-bio" src="http://placehold.it/150" style="margin-bottom:10px;">
             <h4 class="">Name 4</h4>

        </div>
    </div>
    <div class="row">
        <div class="col-xs-3 col-sm-3 col-md-3">
            <img class="img-responsive img-team-bio" src="http://placehold.it/150" style="margin-bottom:10px;">
             <h4 class="">Name 5</h4>

        </div>
    </div>
    <div id="collapseOne" class="col-md-12 panel-collapse collapse" aria-expanded="false" style="height: 0px;">
        <!-- div to hide-->
        <p class="">This is for Person 1.</p>
    </div>
    <!--div to hide-->
    <div id="collapseTwo" class="panel-collapse collapse col-md-12">
        <!-- div to hide-->
        <p class="">This is for Person 2.</p>
    </div>
    <!--div to hide-->
</div>

Bootstrap 的 jQuery Collapse 功能:

var Collapse = function (element, options) {
this.$element      = $(element)
this.options       = $.extend({}, Collapse.DEFAULTS, options)
this.$trigger      = $(this.options.trigger).filter('[href="#' + element.id + '"], [data-target="#' + element.id + '"]')
this.transitioning = null

if (this.options.parent) {
  this.$parent = this.getParent()
} else {
  this.addAriaAndCollapsedClass(this.$element, this.$trigger)
}

if (this.options.toggle) this.toggle()
}

Collapse.VERSION  = '3.3.0'

Collapse.TRANSITION_DURATION = 350

Collapse.DEFAULTS = {
toggle: true,
trigger: '[data-toggle="collapse"]'
}

Collapse.prototype.dimension = function () {
var hasWidth = this.$element.hasClass('width')
return hasWidth ? 'width' : 'height'
}

Collapse.prototype.show = function () {
if (this.transitioning || this.$element.hasClass('in')) return

var activesData
var actives = this.$parent && this.$parent.find('> .panel').children('.in, .collapsing')

if (actives && actives.length) {
  activesData = actives.data('bs.collapse')
  if (activesData && activesData.transitioning) return
}

var startEvent = $.Event('show.bs.collapse')
this.$element.trigger(startEvent)
if (startEvent.isDefaultPrevented()) return

if (actives && actives.length) {
  Plugin.call(actives, 'hide')
  activesData || actives.data('bs.collapse', null)
}

var dimension = this.dimension()

this.$element
  .removeClass('collapse')
  .addClass('collapsing')[dimension](0)
  .attr('aria-expanded', true)

this.$trigger
  .removeClass('collapsed')
  .attr('aria-expanded', true)

this.transitioning = 1

var complete = function () {
  this.$element
    .removeClass('collapsing')
    .addClass('collapse in')[dimension]('')
  this.transitioning = 0
  this.$element
    .trigger('shown.bs.collapse')
}

if (!$.support.transition) return complete.call(this)

var scrollSize = $.camelCase(['scroll', dimension].join('-'))

this.$element
  .one('bsTransitionEnd', $.proxy(complete, this))
  .emulateTransitionEnd(Collapse.TRANSITION_DURATION)[dimension](this.$element[0][scrollSize])
}

Collapse.prototype.hide = function () {
if (this.transitioning || !this.$element.hasClass('in')) return

var startEvent = $.Event('hide.bs.collapse')
this.$element.trigger(startEvent)
if (startEvent.isDefaultPrevented()) return

var dimension = this.dimension()

this.$element[dimension](this.$element[dimension]())[0].offsetHeight

this.$element
  .addClass('collapsing')
  .removeClass('collapse in')
  .attr('aria-expanded', false)

this.$trigger
  .addClass('collapsed')
  .attr('aria-expanded', false)

this.transitioning = 1

var complete = function () {
  this.transitioning = 0
  this.$element
    .removeClass('collapsing')
    .addClass('collapse')
    .trigger('hidden.bs.collapse')
}

if (!$.support.transition) return complete.call(this)

this.$element
  [dimension](0)
  .one('bsTransitionEnd', $.proxy(complete, this))
  .emulateTransitionEnd(Collapse.TRANSITION_DURATION)
}

Collapse.prototype.toggle = function () {
this[this.$element.hasClass('in') ? 'hide' : 'show']()
}

Collapse.prototype.getParent = function () {
return $(this.options.parent)
  .find('[data-toggle="collapse"][data-parent="' + this.options.parent + '"]')
  .each($.proxy(function (i, element) {
    var $element = $(element)
    this.addAriaAndCollapsedClass(getTargetFromTrigger($element), $element)
  }, this))
  .end()
}

Collapse.prototype.addAriaAndCollapsedClass = function ($element, $trigger) {
var isOpen = $element.hasClass('in')

$element.attr('aria-expanded', isOpen)
$trigger
  .toggleClass('collapsed', !isOpen)
  .attr('aria-expanded', isOpen)
}

function getTargetFromTrigger($trigger) {
var href
var target = $trigger.attr('data-target')
  || (href = $trigger.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') // strip for ie7

return $(target)
}

【问题讨论】:

  • 请将代码也添加到您的问题中。来自帮助中心:如果可以创建一个可以链接到的问题的实时示例(例如,在 sqlfiddle.com 或 jsbin.com 上),那么就这样做 - 但也要在您的问题本身中包含代码。不是每个人都可以访问外部网站,而且链接可能会随着时间的推移而中断。

标签: twitter-bootstrap-3 show-hide


【解决方案1】:

为了使 bootstrap 手风琴能够正常工作,需要将可折叠元素安装在面板中。然后,您需要覆盖任何您不想要的面板样式。

为了突出显示选择了哪个图像,您可以将“折叠”类添加到切换折叠的元素。之后,您应用一些 CSS 规则,根据此类的存在来格式化这些元素。每当单击一个项目时,bootstrap 都会从该元素中删除折叠的类,并将其添加到面板组中的其他类中。

HTML:

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
  <div class="col-xs-3 col-sm-3 col-md-3">
    <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne" role="tab" class="collapsed">
      <img class="img-responsive img-team-bio" src="http://placehold.it/150" contenteditable="false">
      <h4 class="">Name 1</h4>
    </a>
  </div>
  <div class="col-xs-3 col-sm-3 col-md-3">
    <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="true" aria-controls="collapseTwo" role="tab" class="collapsed">
      <img class="img-responsive img-team-bio" src="http://placehold.it/150" contenteditable="false">
      <h4 class="">Name 2</h4>
    </a>
  </div>
  <div class="col-md-12 panel">
    <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
      <h4 class="">This is for person 1</h4>
    </div>
  </div>
  <div class="col-md-12 panel">
    <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
      <h4 class="">This is for person 2</h4>
    </div>
  </div>
  ...
</div>

CSS:

#accordion .panel { box-shadow:none; border:none; margin:0; }
#accordion a { color:red; }
#accordion a.collapsed { color:#000; }

在此处引导:http://www.bootply.com/2Sq6MHGkJx

【讨论】:

  • 感谢这有效,但如果我在它下面有另一个部分(另一个面板)怎么办?例如,有人单击顶行,显示 div,然后他们单击底行,然后旧 div 消失,然后底部 div 出现在底部。
  • 我不确定你的意思。你能再具体一点吗?例如:“如果我在它下面有另一个部分(另一个面板)怎么办?” - 它是什么'?也许,使用元素 ID 或类。
  • 对此感到抱歉。我实际上只是将我的两个部分放在一个面板中,以便这两个部分可以显示和隐藏内容。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 2019-09-01
  • 2017-10-10
  • 1970-01-01
  • 2013-11-09
相关资源
最近更新 更多