3

この投稿のタイトルは、路地裏のレースで細いフランネルのスカーフとして webdev-hipster と読みます。ごめん。

私はスクリプトの実行時最適化が得意ではないので、次の関数呼び出しの計算がどれほど悪いのか疑問に思っています。大規模なサイトでは実用的ではないことはわかっていますが、私がそれを使用したい場合、jQuery 呼び出しは半ダースのオブジェクトしか返さないため、ボリュームは多くありません。

 Modernizr.load({
    test: Modernizr.borderradius && Modernizr.boxshadow,
    nope: "_/js/polyfills/pie.js",
    complete: function(){
        if(window.PIE){
            $('*').css('box-shadow').each(function(){ PIE.attach(this); });
            $('*').css('border-radius').each(function(){ PIE.attach(this); });
        }
    }
 });

みんな、ありがとう。

4

2 に答える 2

1

これを試して。

 Modernizr.load({
    test: Modernizr.borderradius && Modernizr.boxshadow,
    nope: "_/js/polyfills/pie.js",
    complete: function(){
        if(window.PIE){
            $('*').each(function(){
                var $this = $(this);
                //check if box-shadow or border-radius is applied
                if($this.css('box-shadow') || $this.css('border-radius')){
                    PIE.attach(this);
                }
            });
        }
    }
 });
于 2012-02-15T19:35:41.693 に答える
0

...jQuery 呼び出しは、半ダース以下のオブジェクトを返します...

したがって、半ダースは 6 です。そのうちの 4 つはhtmlheadscript、およびbodyです。:-) ページには他に 2 つの要素しかありませんか?

真剣に、数が非常に少ない場合、それは問題ではありません. ただし、どれが大きなハンマーである$()かではなく、本当に必要な要素のみに呼び出しを制限する必要があります。$("*")

本当にドキュメント全体を処理する必要がある場合は、単純な再帰降下関数を使用します。

function applyPie(element) {
    var node;
    for (node = element.firstChild; node; node = node.nextSibling) {
        if (node.nodeType === 1) { // 1 = element
            node.style.boxShadow = /* ...?... there's nothing in your jQuery call */;
            node.style.borderRadius = /* ...?... there's nothing in your jQuery call */;
            PIE.attach(node);
            applyPie(node);
        }
    }
}

applyPie(document.documentElement);

PIE.attachを除くすべての要素を呼び出しますdocumentElement。、、などにPIE を付けないように nodeName(または)を使用することができます。単純な再帰降下関数を使用すると、メモリ内に大きなフラット配列が作成されるのを回避できます。tagNamehtmlheadstyle$("*")

于 2012-02-15T18:13:43.840 に答える