未捕获的TypeError:无法读取属性'默认值'

时间:2013-06-26 00:57:08

标签: jquery twitter-bootstrap bootstrap-modal

我创建了一个函数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 个答案:

答案 0 :(得分:1)

modal.noConflict返回对modal函数的引用,并在加载模式插件之前将$.fn.modal还原为的任何内容。

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

最简单的解决方案可能是调用noConflict,因为您的异常显示$.fn.modal未定义且可能与任何内容不冲突。

另一个解决方案,正如@fat在关于链接错误的评论中建议的那样,将使用jquery-ui的bridge函数。