使用 jquery 选择最多 5 li 项
我有一个
您可以在这里看到这个:
,里面有很多
<ul>
<li><img src="content/img/logos/pon.jpg" alt="PON" width="111" height="63" /></li>
<li><img src="content/img/logos/spo.png" alt="SPO Utrecht" width="130" height="67" /></li>
<li><img src="content/img/logos/campus.jpg" alt="Onderwijs Campus" width="137" height="86" /></li>
<li><img src="content/img/logos/cpwb.png" alt="CPWB" width="112" height="99"/></li>
<li><img src="content/img/logos/expertis.jpg" alt="Expertis" width="120" height="56" /></li>
<li><img src="content/img/logos/inos.jpg" alt="Inos" width="211" height="67" /></li>
<li><img src="content/img/logos/OSG.jpg" alt="OSG" width="130" height="51" /></li>
<li><img src="content/img/logos/pio.png" alt="Pio" width="138" height="92" /></li>
<li><img src="content/img/logos/Signum.png" alt="Signum" width="181" height="68" /></li>
<li><img src="content/img/logos/vgs.png" alt="VGS" width="192" height="63" /></li>
</ul>
但没有我的问题。 li 项目有自己的 标签。但我想要让 jquery 显示 5 li 项目。我怎样才能用 javascript / jquery.他给我随机展示 5 个这样的项目?
谢谢
I have a <ul>
with a lot of <li>
You can see this here:
<ul>
<li><img src="content/img/logos/pon.jpg" alt="PON" width="111" height="63" /></li>
<li><img src="content/img/logos/spo.png" alt="SPO Utrecht" width="130" height="67" /></li>
<li><img src="content/img/logos/campus.jpg" alt="Onderwijs Campus" width="137" height="86" /></li>
<li><img src="content/img/logos/cpwb.png" alt="CPWB" width="112" height="99"/></li>
<li><img src="content/img/logos/expertis.jpg" alt="Expertis" width="120" height="56" /></li>
<li><img src="content/img/logos/inos.jpg" alt="Inos" width="211" height="67" /></li>
<li><img src="content/img/logos/OSG.jpg" alt="OSG" width="130" height="51" /></li>
<li><img src="content/img/logos/pio.png" alt="Pio" width="138" height="92" /></li>
<li><img src="content/img/logos/Signum.png" alt="Signum" width="181" height="68" /></li>
<li><img src="content/img/logos/vgs.png" alt="VGS" width="192" height="63" /></li>
</ul>
But no my question. The li items have his own <img>
tag. But i want make, that jquery show me 5 li items. How can i make with javascript / jquery. That he show me random 5 of this li items?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
演示
demo
位于 jQuery:选择随机元素。
至于OP在评论中提出的问题:(
Demo)
Found in jQuery: select random elements.
As for the question posed by OP in the comments:
(Demo)
当您能够引用 li 时,您将能够迭代它的子项。哪些是李氏。
示例:
在上面的示例中,您可以使用 $(this) 引用 li。
你可以做的是将它们存储在一个数组中并随机获取 5 个。您可以使用 Math.random 方法来完成此操作。之后重建 ul li 列表或使用 jQuery 删除不需要的项目。
When you are able to reference the li's, you'll be able to iterate it's children. Which are the li's.
Example:
In the example above, you can reference to the li's with $(this).
What you could do is store them in an array and fetch 5 randomly. You could do this using the Math.random method. Either rebuild the ul li list afterwards or remove the unwanted items with jQuery.