2013-06-26 70 views
1

我犯了一个功能appendScript将上一个按钮单击事件被称为我的功能代码是遗漏的类型错误:无法读取属性“默认”

function appendScript() { 
    var v_js; 
    var head = document.getElementsByTagName('head')[0]; 
    v_js = document.createElement('script'); 
    v_js.type = 'text/javascript'; 
    v_js.src = "/resource/1372205606000/jquery_min_js"; 
    head.appendChild(v_js); 
    interval = self.setInterval(function() { 
     if (jQuery) { 
      window.clearInterval(interval); 

      var body = document.getElementsByTagName('body')[0]; 
      v_js = document.createElement('script'); 
      v_js.type = "text/javascript"; 

      v_js.src = "/resource/1372176744000/bootstrap_min_js"; 
     } 
     body.appendChild(v_js); 
     var v_css = document.createElement('link'); 
     v_css.rel = "stylesheets"; 
     v_css.type = "text/css"; 
     v_css.href = "/resource/1372206945000/bootstrap_min_css"; 
     body.appendChild(v_css); 
    }, 300); 

    var interval1 = self.setInterval(function() { 

     if (typeof (jQuery.fn.modal) !== 'undefined') { 
      console.log('Hello!!'); 
      window.clearInterval(interval1); 
      jQuery(document.getElementsByTagName('body')[0]).append('<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3 id="myModalLabel">Modal header</h3></div><div class="modal-body"><p>One fine body…</p></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">Close</button><button class="btn btn-primary">Save changes</button></div></div>'); 
      var bootstrapModal = $.fn.modal.noConflict(); // return $.fn.button to previously assigned value 
      $.fn.bootstrapModal = bootstrapModal; 
      $('#myModal').bootstrapModal('show'); 
     } 
    }, 300); 
} 

和我面临的误差

Uncaught TypeError: Cannot read property 'defaults' of undefined bootstrap_min_js:6 
(anonymous function) bootstrap_min_js:6 
x.extend.each jquery_min_js:4 
x.fn.x.each jquery_min_js:4 
e.fn.modal bootstrap_min_js:6 
(anonymous function) 

回答

1

modal.noConflict返回对modal函数的引用,并将$.fn.modal恢复为,然后加载模式插件

但是,新定义的modal函数实际上使用了$.fn.modal.defaults。谷歌搜索显示,这是一个unresolved and closed bug

最简单的解决方案可能是呼叫noConflict,因为您的异常所揭示的,$.fn.modal没有定义,并且可能不与任何冲突。

另一种解决方案,就像@fat在对链接bug的评论中所暗示的那样,将使用jquery-ui的bridge函数。

相关问题