0

このコードのように、var "el"に対して未定義を返します

$('.element').each(function(){
    var el = $(this);
    setTimeout(function(el) {
        el.css({ marginTop: -100 });
    }, 550);
    $(window).resize(function(el) {
        setTimeout(function(el) {
            el.css({ marginTop: -100 }); //this is where el is undefined
        }, 550);            
    })
});

私は何が間違っているのですか?

4

3 に答える 3

3

el関数の引数としてのせいだと思います。それらを削除してみてください。

$('.element').each(function(){
    var el = $(this);
    setTimeout(function() {
        el.css({ marginTop: -100 });
    }, 550);
    $(window).resize(function() {
        setTimeout(function() {
            el.css({ marginTop: -100 }); //this is where el is undefined
        }, 550);            
    })
});

私が間違っていたら、私を十字架につけることを忘れないでください:P

編集:何が起こっているのかを説明するために、functioninresizeはオブジェクトに渡されるため、設定した変数がevent上書きされます。はオブジェクトではないため、関数を呼び出すことはできません。elEventjQuerycss

于 2013-10-03T13:47:27.737 に答える