基于集合中lis数的li的动态宽度 [英] dynamic width of li based on number of lis in a set
问题描述
我想根据列表中的li(s)数量将动态宽度设置为li(s)。例如,如果有18个li(s)。每个宽度应为100%/ 18 = 5.55%。如果我有3组不同的ul,我该怎么办?我尝试使用它。
I want to set dynamic width to li(s) based on how many li(s) are there in list. For example if there are 18 li(s). Each width should be 100% / 18 = 5.55%. Also what should I do if I would have 3 different sets of ul. I tried to use this.
$('.lesson-nav ul li').width( 100 / $(".lesson-nav ul li").length + '%' );
原因不明:此脚本的宽度为120%,而当时只有1 li和27.55%是18 lis。
For unknown reason: This script gives width 120% while there is only 1 li and 27.55% when there are 18 lis.
推荐答案
根据文档:
.css(width)和.width()之间的区别在于后者
返回无单位像素值(例如,400),而前
返回单位完整的值(例如,400px)。
The difference between .css(width) and .width() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px).
所以你应该使用 css(宽度)
$('.lesson-nav ul li').css("width", 100 / $(".lesson-nav ul li").length + '%' );
这篇关于基于集合中lis数的li的动态宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!