gpt4 book ai didi

javascript - 你好,你能帮我在 Javascript 中添加最多 5 个列表项选择吗?

转载 作者:行者123 更新时间:2023-12-03 10:36:41 24 4
gpt4 key购买 nike

所以我是 JS 的绝对初学者,但希望有一个用户可以从最多 5 个选项中进行选择的列表,这些选项出现在单独的 div 中。正如你所看到的,我已经成功地让一个点击即可工作。

<ul id="products">
<li>Bell Box Labels</li>
<li>Camera Labels</li>
<li>CCTV Signs</li>
<li>Certificates & Job Books</li>
<li>Extinguisher Labels</li>
<li>Fascia Overlays</li>
<li>Fire Exit Signage</li>
<li>Panel Labels</li>
<li>Serviced By Labels</li>
<li>Warning Boards</li>
<li>Window Stickers</li>
</ul>




<script type="text/javascript">

var lis = document.getElementById("products").getElementsByTagName('li');

for (var i=0; i<lis.length; i++) {
lis[i].addEventListener('click', sampleChoice);
}


function sampleChoice() {
document.getElementById("selection").innerHTML = this.innerHTML;

}


</script>

最佳答案

这对我有用

<ul id="products">
<li>Bell Box Labels</li>
<li>Camera Labels</li>
<li>CCTV Signs</li>
<li>Certificates & Job Books</li>
<li>Extinguisher Labels</li>
<li>Fascia Overlays</li>
<li>Fire Exit Signage</li>
<li>Panel Labels</li>
<li>Serviced By Labels</li>
<li>Warning Boards</li>
<li>Window Stickers</li>
</ul>
<button onclick="resetSelection()">reset</button>
<div id="selection"></div>



<script type="text/javascript">

var lis = document.getElementById("products").getElementsByTagName('li');

for (var i=0; i<lis.length; i++) {
lis[i].addEventListener('click', sampleChoice);
}

var count=0;
function sampleChoice() {
if(count<5)
{
document.getElementById("selection").innerHTML+= this.innerHTML;
count++;
}}
function resetSelection()
{
count=0;
document.getElementById("selection").innerHTML= "";

}

关于javascript - 你好,你能帮我在 Javascript 中添加最多 5 个列表项选择吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28958823/

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