在 jQuery 中使用 * 选择器将 CSS3PIE 与 Modernizr 一起应用

发布于 2025-01-06 03:07:18 字数 543 浏览 8 评论 0原文

这篇文章的标题读作“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); });
        }
    }
 });

谢谢大家。

The title of this post reads as webdev-hipster as a skinny flannel scarf at an alleycat race. Sorry.

I'm not great with script runtime optimization, so I'm wondering just how bad computationally the following function call is going to be. I know it wouldn't be practical for a big site, but where I wan to use it, the jQuery call is going to return no more than a half dozen objects, so the volume isn't high.

 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); });
        }
    }
 });

Thanks everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不气馁 2025-01-13 03:07:18

试试这个。

 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);
                }
            });
        }
    }
 });

Try this.

 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);
                }
            });
        }
    }
 });
人生戏 2025-01-13 03:07:18

...jQuery 调用将返回不超过六个对象...

所以六个就是六个。其中四个是 htmlheadscriptbody。 :-) 页面上只有两个其他元素?

说实话,如果数量很少,那也没关系。不过,您希望将 $() 调用限制为仅真正需要它的元素,而不是像大锤子那样的 $("*")

如果您确实需要遍历整个文档,请使用简单的递归下降函数:

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);

该函数对除 documentElement 之外的每个元素调用 PIE.attach。您可以使用 nodeName (或 tagName),这样就不会将 PIE 附加到 htmlhead风格,等等。使用简单的递归下降函数可以避免在内存中创建大型平面数组,这正是 $("*") 的作用。

...the jQuery call is going to return no more than a half dozen objects...

So half a dozen is six. Four of those will be html, head, script, and body. :-) You only have two other elements on the page?

Seriously, if the number is very low, it doesn't matter. You'd want to limit the $() call to only the elements that really need it, though, rather than $("*") which is a big hammer.

If you really need to run through the whole document, use a simple recursive-descent function:

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);

That calls PIE.attach on every element except the documentElement. You might use nodeName (or tagName) so you don't attach PIE to html, head, style, and such. Using a simple recursive-descent function avoids creating large flat arrays in memory, which is what $("*") does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文