应用 Cufon 之前检查元素是否存在
我有一个元素数组 #document h1, #content h2
我知道它们可能出现在我网站的整个页面上;一些页面不是全部。
我正在做的是迭代数组并检查元素是否存在 - 如果不存在,我就从数组中拼接它。然后,我使用 toString
方法将剩余元素传递给 Cufon
。
<script>
$(function(){
var eurostyle = ["#container h1","#content h2","#content h3","#content h4","#content .sidebar ul span", "#sitenav ul.menu span"];
for (i=eurostyle.length-1;i >=0;i--) {
if (!$(eurostyle[i]).length) {
eurostyle.splice(i,1);
}
}
Cufon.replace(eurostyle.toString(),{fontFamily: "Eurostile"});
});
</script>
我很好奇这种技术是否值得?
它是否会提高性能,或者检查每个元素是否存在实际上会降低浏览器的速度?
I have an array of elements #document h1, #content h2
which I know may be present on pages throughout my site; some pages not all.
What I'm doing is iterating through the array and checking if the element exists - if it doesn't I splice it from the array. I then use the toString
method to pass the remaining elements to Cufon
.
<script>
$(function(){
var eurostyle = ["#container h1","#content h2","#content h3","#content h4","#content .sidebar ul span", "#sitenav ul.menu span"];
for (i=eurostyle.length-1;i >=0;i--) {
if (!$(eurostyle[i]).length) {
eurostyle.splice(i,1);
}
}
Cufon.replace(eurostyle.toString(),{fontFamily: "Eurostile"});
});
</script>
I'm curious if this technique is worth it?
Does it improve performance or does checking to see if each element exists, actually slow the browser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cufon 已经使用 jquery 的选择器引擎检查它们是否存在(假设它存在)。选择器越简单,您使用的处理能力就越少。
不幸的是,遵循这条道路可能会导致非常糟糕的标记。
我的想法是,你的代码只是与 cufon 重复。
Cufon already checks if they exist using jquery's selector engine (assuming it is present). The simpler the selector the less processing power you will use.
Unfortunately following this path may lead to very krufty markup.
My thoughts are that your code is just duplicating with cufon does anyway.