gpt4 book ai didi

javascript - li 的动态宽度基于集合中的 lis 数量

转载 作者:行者123 更新时间:2023-11-29 14:51:56 25 4
gpt4 key购买 nike

我想根据列表中有多少 li(s) 将动态宽度设置为 li(s)。例如,如果有 18 个 li(s)。每个宽度应为 100%/18 = 5.55%。另外,如果我有 3 套不同的 ul,我该怎么办。我试着用这个。

$('.lesson-nav ul li').width( 100 / $(".lesson-nav ul li").length + '%' );

未知原因:此脚本在只有 1 里时给出宽度 120%,在有 18 里时给出 27.55%。

最佳答案

根据 documentation :

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(width)

$('.lesson-nav ul li').css("width", 100 / $(".lesson-nav ul li").length + '%' );

关于javascript - li 的动态宽度基于集合中的 lis 数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24328336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com