gpt4 book ai didi

javascript - 根据数组中的值检查每个 div 的 id

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

我正在使用以下 JS 根据一组现有类别创建一组选项卡。 我需要将其扩展为基于 JS 数组中的值来定位 DIV id 中的特定 id。

        $("#categories div[id^=category]:not(:first)", this).hide();

var cats = $('#categories div[id^=category]');

cats.each(function () {
var anch = $(this).find('h3 a').eq(1).clone();
anch[0].rel = this.id;
$('<li/>').append(anch).appendTo('#tabs');
});

html:

<div id="category_1">
<h3 class="maintitle">
<a class="toggle">..</a>
<a href="#">Cat 1 Title</a>
</h3>
<div>
...
</div>
</div>

<div id="category_2">
<h3 class="maintitle">
<a class="toggle">..</a>
<a href="#">Cat 2 Title</a>
</h3>
<div>
...
</div>
</div>

我已经通过添加准备好了一个 JS 数组:

var catsList = '{$cats}'; // comma separated list of numbers generated in PHP - returns 1,4,8 currently.
var catsArray = catsList.split(',');

我将如何转换以下内容,以检查 catsArray 中的每个项目?

var cats = $('#categories div[id^=category]');

有点像

var cats = $('#categories div[id^=category_'+catsArray+']');

但显然检查数组中的每个项目而不是整个数组。

最佳答案

您可以使用它,因为 ID 在上下文页面上必须是唯一的:

var cats = $('#category_'+catsArray.join(',#category_'));

DEMO

关于javascript - 根据数组中的值检查每个 div 的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17445440/

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