2014-03-19 28 views
1

我不明白为什么我收到以下错误: 无法调用“scrollTop的”未定义引导模式:不能调用方法“scrollTop的”未定义

当我点击,显示模式的链接。

我唱jQuery 1.11,bootstrap 3.1.1。

有关显示模式的代码(HAML)

按钮:

.forgot_password.pull-right = link_to t('.forgot_password'), '#forgot_password', data: { target: "#forgot_password", toggle: "modal" }, tabindex: 5

模态:

#forgot_password.modal{tabindex: -1, role: 'dialog', "aria-labelledby" => t('.title'), "aria-hidden" => true} 
    .modal-dialog 
    .modal-content 
     .modal-header 
     %button.close{"aria-hidden" => "true", "data-dismiss" => "modal", :type => "button"} × 
     %h4= t('.title') 
     .modal-body 
     .form_wrapper 
      .innertxt= t('.explanation') 
      .forgot_password_form 
      = form_for :forgot_password, url: forgot_password_path do |f| 
       = text_field_tag :email, '', placeholder: t('email'), size: 50, autofocus: true 
       = submit_tag t('send'), :class => 'btn' 

引导其中的问题发生:(方法Modal.prototype 。展示

this.backdrop(function() { 
     var transition = $.support.transition && that.$element.hasClass('fade') 

     if (!that.$element.parent().length) { 
     that.$element.appendTo(document.body) // don't move modals dom position 
     } 

     that.$element 
     .show() 
     .scrollTop(0) 
... 

显示的错误:

TypeError: 'undefined' is not an object (evaluating 'that.$element 
       .show() 
       .scrollTop') 

我想这that.element为空或未定义,它打破了代码。但是我正在寻找修补程序/解决方法,因为它违反了我的测试规范! (红宝石与水豚)

我跟着http://getbootstrap.com/javascript/#modals的例子,到目前为止,我没有看到他们和我的代码之间的任何区别。我试图用JavaScript代替html来打开模式,但它完全一样。

有什么想法?

编辑:红宝石/水豚代码

click_link 'Glemt adgangskode?'# Forgotten password? 
    sleep 3 
    within_frame('form_login') do 
    fill_in 'email', with: '[email protected]' 
    click_button 'Send' 
    end 

EDIT2:顺便说一下,一切正常,该模式弹出正确的,我刚刚得到一个JavaScript错误实际上并不影响用法。但我想了解并解决这个问题。

回答

3

好吧,那是我的错!

其实,我在几周前重写了jQuery.show()方法,并忘记了...... return声明。这就是scrollTop实际上基于undefined元素的原因!

$(function(){ 
    /** 
    * Override hide function. 
    */ 
    // Store a reference to the original remove method. 
    var originalShowMethod = $.fn.show; 

    $.fn.show = function(){ 
    var self = $(this); 

    // Remove CSS classes that hide the element. 
    self.removeClass('hidden hide invisible'); 

    // Apply the original method. 
    return originalShowMethod.apply(this, arguments); 
    } 
}); 

现在效果更好!重写允许我在调用show()函数时自动删除CSS类,以避免每次重复相同的东西! 对不起!

相关问题