【问题标题】:Stop anchor element from reloading and/or scrolling to top阻止锚元素重新加载和/或滚动到顶部
【发布时间】:2013-07-30 23:44:45
【问题描述】:

当有人单击#collapseOne 锚点时,页面会重新加载并滚动到顶部。我需要禁用它。但是,我无法将 href 更改为其他任何内容,因为它用于网站的功能。有什么方法可以阻止网站滚动到顶部或完全阻止它重新加载?

  <div class="accordion" id="accordion2">
  <div class="accordion-group">
  <div class="accordion-heading">
  <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
    Collapsible Group Item #1
  </a>
 </div>
 <div id="collapseOne" class="accordion-body collapse in">
  <div class="accordion-inner">
    Anim pariatur cliche...
  </div>
   </div>
 </div>
 <div class="accordion-group">
  <div class="accordion-heading">
  <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
    Collapsible Group Item #2
  </a>
 </div>
 </div>

这里是 JavaScript。这是 Twitter 引导程序。

!function ($) {

"use strict"; // jshint ;_;


/* COLLAPSE PUBLIC CLASS DEFINITION
 * ================================ */

var Collapse = function (element, options) {
this.$element = $(element)
this.options = $.extend({}, $.fn.collapse.defaults, options)

if (this.options.parent) {
  this.$parent = $(this.options.parent)
}

this.options.toggle && this.toggle()
}

Collapse.prototype = {

constructor: Collapse

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

, show: function () {
  var dimension
    , scroll
    , actives
    , hasData

  if (this.transitioning || this.$element.hasClass('in')) return

  dimension = this.dimension()
  scroll = $.camelCase(['scroll', dimension].join('-'))
  actives = this.$parent && this.$parent.find('> .accordion-group > .in')

  if (actives && actives.length) {
    hasData = actives.data('collapse')
    if (hasData && hasData.transitioning) return
    actives.collapse('hide')
    hasData || actives.data('collapse', null)
  }

  this.$element[dimension](0)
  this.transition('addClass', $.Event('show'), 'shown')
  $.support.transition && this.$element[dimension](this.$element[0][scroll])
 }

, hide: function () {
  var dimension
  if (this.transitioning || !this.$element.hasClass('in')) return
  dimension = this.dimension()
  this.reset(this.$element[dimension]())
  this.transition('removeClass', $.Event('hide'), 'hidden')
  this.$element[dimension](0)
}

, reset: function (size) {
  var dimension = this.dimension()

  this.$element
    .removeClass('collapse')
    [dimension](size || 'auto')
    [0].offsetWidth

  this.$element[size !== null ? 'addClass' : 'removeClass']('collapse')

  return this
}

, transition: function (method, startEvent, completeEvent) {
  var that = this
    , complete = function () {
        if (startEvent.type == 'show') that.reset()
        that.transitioning = 0
        that.$element.trigger(completeEvent)
      }

  this.$element.trigger(startEvent)

  if (startEvent.isDefaultPrevented()) return

  this.transitioning = 1

  this.$element[method]('in')

  $.support.transition && this.$element.hasClass('collapse') ?
    this.$element.one($.support.transition.end, complete) :
    complete()
}

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

}


/* COLLAPSE PLUGIN DEFINITION
* ========================== */

var old = $.fn.collapse

$.fn.collapse = function (option) {
return this.each(function () {
  var $this = $(this)
    , data = $this.data('collapse')
    , options = $.extend({}, $.fn.collapse.defaults, $this.data(), typeof option == 'object' && option)
  if (!data) $this.data('collapse', (data = new Collapse(this, options)))
  if (typeof option == 'string') data[option]()
})
}

$.fn.collapse.defaults = {
toggle: true
}

$.fn.collapse.Constructor = Collapse


/* COLLAPSE NO CONFLICT
* ==================== */

$.fn.collapse.noConflict = function () {
$.fn.collapse = old
return this
}


/* COLLAPSE DATA-API
* ================= */

$(document).on('click.collapse.data-api', '[data-toggle=collapse]', function (e) {
var $this = $(this), href
  , target = $this.attr('data-target')
    || e.preventDefault()
    || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
  , option = $(target).data('collapse') ? 'toggle' : $this.data()
$this[$(target).hasClass('in') ? 'addClass' : 'removeClass']('collapsed')
$(target).collapse(option)
})

}(window.jQuery);

【问题讨论】:

  • 请提供您的 JS 代码 :)。您的点击事件中可能缺少e.preventDefault();。点击事件代码就可以了。
  • 刚刚添加了JS。这是 Twitter 引导程序。我会复制更少的代码,但我不确定您需要查看哪个部分。
  • 不管你使用什么,当你想禁用链接的自然行为时..总是使用javascript:void(0)..检查答案,祝你项目好运

标签: javascript jquery css


【解决方案1】:
$('a.accordion-toggle').on('click', function(e) {
    // whatever other code this click event needs to run here...
    e.preventDefault();
}

e 是事件对象,调用preventDefault 方法会阻止它的正常行为(在这种情况下重新加载页面并重置滚动位置)。您可能需要将此与现有的点击处理代码结合起来才能正常工作(显然您没有提供所有代码)。

return false 的工作方式也类似于e.preventDefault()(实际上它只是运行e.preventDefault 和e.stopPropagation,它可以防止点击事件传播到DOM 中的父元素)。

【讨论】:

    【解决方案2】:

    javascript:void(0)

    我总是建议你不要使用preventDefault(跨浏览器问题)。 return false 也没有那么依赖。

    我过去也遇到过同样的问题。我求助于这个,因为它适用于所有浏览器!!!

    $('#my_herf_id').on('click', function() {
           // code..
        $("#my_herf_id").attr('href','javascript:void(0)');
    }
    

    【讨论】:

    • 他特别指出href的值不能改变。否则,是的,这是一种比preventDefault 或return false 更可靠的方法来防止默认锚行为。
    • 这里没有任何变化...op得到他想要的......它的作用与阻止默认对href的作用相同......100% surity
    • 我试过了,但运气不好。我做对了吗?
    • @user2635166: 将其提供给链接s id,you havent assigned an id to &lt;a class="accordion-toggle" ...with $('collapseOne')` 您正在选择 div.not a link..this will work 100 %
    • 找不到合适的放置位置。猜猜我没有足够的经验,即使有正确的答案.. 还是谢谢!
    猜你喜欢
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 2011-06-18
    • 2019-09-30
    • 2011-08-13
    相关资源
    最近更新 更多