繁体   English   中英

jQuery代码fancybox第二次显示错误

[英]jquery code fancybox showing an error 2nd time

我有我的追随者,它的工作很好,第一次,但是下次,如果我尝试打开,我会收到一个错误

/* select 2 */
    $("#otherlogins").niceSelect();

    $(document).on("change",".socialselection",function() {
        var e = $(this);
        var mode = $('div.nice-select.socialselection span.current').text();
        if(e.val() == '') {
            jAlert('warning', 'Please Choose the Service which you want to use for Login', 'Oops');
        } else {
            $('<link/>', {rel: 'stylesheet',type: 'text/css',href: '/js/fancybox/jquery.fancybox-1.3.4.css'}).appendTo('head'); 
            var script_arr = [
                'jquery.fancybox-1.3.4.pack.js', 
                'jquery.mousewheel-3.0.4.pack.js', 
                'jquery.easing-1.3.pack.js'
            ];
            $.getMultiScripts(script_arr, '/js/fancybox/').done(function() {
                $.fancybox({
                    'width'         : '75%',
                    'height'        : '75%',
                    'autoScale'     : false,
                    'transitionIn'  : 'none',
                    'transitionOut' : 'none',
                    'type'          : 'iframe',
                    'href'          : 'openlogin.cfm?mode='+ mode
                });
            });
        }   
    });

问题似乎在需要的时候我可能会在哪里装载fancybox,但是我对为什么它表现出这种方式有点迷惑

多选功能

$.getMultiScripts = function(arr, path) {
        var _arr = $.map(arr, function(scr) {
            return $.getScript( (path||"") + scr );
        });

        _arr.push($.Deferred(function( deferred ){
            $( deferred.resolve );
        }));

        return $.when.apply($, _arr);
    }

我第二次选择它时得到的错误是

Uncaught TypeError: Cannot read property 'hide' of undefined
    at N (eval at <anonymous> (jquery.min.js:2), <anonymous>:18:284)
    at I (eval at <anonymous> (jquery.min.js:2), <anonymous>:19:43)
    at Function.b.fancybox (eval at <anonymous> (jquery.min.js:2), <anonymous>:38:325)
    at Array.<anonymous> (js.cfm:622)
    at j (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at Object.<anonymous> (jquery.min.js:2)
    at j (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at x (jquery.min.js:4)
N @ VM362:18
I @ VM362:19
b.fancybox @ VM362:38
(anonymous) @ js.cfm:622
j @ jquery.min.js:2
fireWith @ jquery.min.js:2
(anonymous) @ jquery.min.js:2
j @ jquery.min.js:2
fireWith @ jquery.min.js:2
x @ jquery.min.js:4
b @ jquery.min.js:4
XMLHttpRequest.send (async)
send @ jquery.min.js:4
ajax @ jquery.min.js:4
$.ajax @ VM146:1
m.<computed> @ jquery.min.js:4
getScript @ jquery.min.js:4
(anonymous) @ rfactor.cfm:105
map @ jquery.min.js:2
$.getMultiScripts @ js.cfm:104
(anonymous) @ js.cfm:621
dispatch @ jquery.min.js:3
r.handle @ jquery.min.js:3
trigger @ jquery.min.js:3
(anonymous) @ jquery.min.js:3
each @ jquery.min.js:2
each @ jquery.min.js:2
trigger @ jquery.min.js:3
(anonymous) @ jquery.nice-select.min.js:4
dispatch @ jquery.min.js:3
r.handle @ jquery.min.js:3

不知道问题出在哪里,

有什么问题,我不确定

看,js是异步的,我什至不确定在事件中使用var定义变量时,该变量将是uniqe变量(因此,在下一个事件之后,dom中每个变量都有一个以上),并且有时赎金之类的东西...尝试以下方法:

$(document).on("change",".socialselection",function()
{
    //let e = $(this); //you dont need this variable in this case
    let mode = $('div.nice-select.socialselection span.current').text();
    if(!$(this).val().length) //<-- I think this solution better for you but:*1
    {
        //...
    } else {
        //...
        let script_arr = //...

// ... * 1:当val()null''undefined0 (js不是某种类型的安全lang,这会使测试变得困难val()时, if(!$(this).val().length)将捕获), 永远不要使用这种形式: if(!$(this).val().length > 0)因为当值为null或undefined时会抛出类似您的答案的错误(我想... js专家,真的不在乎此lang,只需尝试帮助) ...

$.getMultiScripts = function(arr, path) {
    let _arr = $.map(arr, function(scr) {
        return $.getScript( (path||"") + scr );
    });

    _arr.push($.Deferred(function( deferred ){
        $( deferred.resolve );
    }));

    return $.when.apply($, _arr);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM