gpt4 book ai didi

javascript - 我没有得到我想要的结果

转载 作者:行者123 更新时间:2023-11-30 12:10:55 25 4
gpt4 key购买 nike

我试图根据从列表中选择的选项显示结果,但没有得到结果。我想当我点击“选项 1”时,“答案 1”将出现,其他答案将被隐藏,并且作为我选择“选项 2”,然后显示“答案 2”,否则所有答案都将被隐藏,依此类推...

HTML:

<div id="amenities" target="1">option 1</div>
<div id="amenities" target="2">option 2</div>
<div id="amenities" target="3">option 3</div>
<div id="amenities" target="4">option 4</div>
<div id="amenities" target="5">option 5</div>
<div id="amenities" target="5">option 5</div>

<div id="list" class="amenities-list">answer 1</div>
<div id="list" class="amenities-list">answer 2</div>
<div id="list" class="amenities-list">answer 3</div>
<div id="list" class="amenities-list">answer 4</div>
<div id="list" class="amenities-list">answer 5</div>
<div id="list" class="amenities-list">answer 6</div>

JQuery:

jQuery(function(){
jQuery('#amenities').click(function(){
jQuery('.amenities-list').hide();
jQuery('#list'+$(this).attr('target')).show();
});
});

CSS:

.amenities-list{display: none;}

最佳答案

通过向它们添加数字来编辑 div id。还将选项的 id 更改为类。

<div class="amenities" target="1">option 1</div>
<div class="amenities" target="2">option 2</div>
<div class="amenities" target="3">option 3</div>
<div class="amenities" target="4">option 4</div>
<div class="amenities" target="5">option 5</div>
<div class="amenities" target="5">option 5</div>

<div id="list1" class="amenities-list">answer 1</div>
<div id="list2" class="amenities-list">answer 2</div>
<div id="list3" class="amenities-list">answer 3</div>
<div id="list4" class="amenities-list">answer 4</div>
<div id="list5" class="amenities-list">answer 5</div>
<div id="list6" class="amenities-list">answer 6</div>

并编辑你的 jquery 以获得类:

jQuery(function(){
jQuery('.amenities').click(function(){
jQuery('.amenities-list').hide();
jQuery('#list'+$(this).attr('target')).show();
});
});

关于javascript - 我没有得到我想要的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33815872/

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