jQuery-如何计算子项并应用基于百分比的宽度 [英] jQuery - How do I count children and apply percentage based width
本文介绍了jQuery-如何计算子项并应用基于百分比的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该如何使用jQuery来计算div id #foo中的子元素(在本例中为列表元素)的数量,然后除以100/(已计数的子元素数量).最后一步是将其应用为基于百分比的宽度
How can I use jQuery to count the number of child elements (in this case list elements) inside div id #foo and then divide 100 / (# of counted child elements). The last step would be to apply that as a percentage based width
<style="width: Npx">
N = [100/(子元素数)]在哪里?
Where N = [100/(# of child elements)]?
推荐答案
尝试一下(在foo中没有子节点的情况下,要注意大小写):
Try this(it takes care of case when there are not children in foo):
var el = $('#foo');
var len = el.children().length;
if(len > 0){
len = 100/len;
el.css("width", len + "px");
}
这篇关于jQuery-如何计算子项并应用基于百分比的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文