1

ここでどこが間違っているのか教えてもらえますか? Firefox と Chrome でテストしましたが、IE8 で動作するだけで問題なく動作します。

        setTimeout(function(it) {
            x = $('.menuheader:first-child').position().left;
            w = $('.menuheader:first-child').width();
            p = x + w + 16;
            $(it).next().css('left', p);
            $(it).next().show();
        }, 200, this);

また、試してみました...

        function showmenu(it){
            x = $('.menuheader:first-child').position().left;
            w = $('.menuheader:first-child').width();
            p = x + w + 16;
            $(it).next().css('left', p);
            $(it).next().show();
        }

        window.setTimeout(function() {
            showmenu(this)
        }, 200);
4

2 に答える 2

2

それらを受け入れることができない関数に「パラメーターを渡す」ための正しい従来の方法は、クロージャーを使用することです。

var that = this;
setTimeout(function(){ doStuff(that);}, 200);

function doStuff(it) {
   x = $('.menuheader:first-child').position().left;
   w = $('.menuheader:first-child').width();
   p = x + w + 16;
   $(it).next().css('left', p);
   $(it).next().show();
}

新しい代替手段 (ポリフィルのない IE8 とは互換性がありません):

 setTimeout(doStuff.bind(null, this), 200);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

于 2013-10-08T09:51:35.640 に答える
1

setTimeoutパラメータが特に信頼できるとは思っていないので、次のようにします。

var it = this;
setTimeout(function() { ... }, 200);
于 2013-10-08T09:42:13.273 に答える