在 jQuery 中使用 * 选择器将 CSS3PIE 与 Modernizr 一起应用
这篇文章的标题读作“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个。
Try this.
所以六个就是六个。其中四个是
html
、head
、script
和body
。 :-) 页面上只有两个其他元素?说实话,如果数量很少,那也没关系。不过,您希望将
$()
调用限制为仅真正需要它的元素,而不是像大锤子那样的$("*")
。如果您确实需要遍历整个文档,请使用简单的递归下降函数:
该函数对除
documentElement
之外的每个元素调用PIE.attach
。您可以使用nodeName
(或tagName
),这样就不会将 PIE 附加到html
、head
、风格
,等等。使用简单的递归下降函数可以避免在内存中创建大型平面数组,这正是$("*")
的作用。So half a dozen is six. Four of those will be
html
,head
,script
, andbody
. :-) 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:
That calls
PIE.attach
on every element except thedocumentElement
. You might usenodeName
(ortagName
) so you don't attach PIE tohtml
,head
,style
, and such. Using a simple recursive-descent function avoids creating large flat arrays in memory, which is what$("*")
does.