3 回答

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊
`所以,根據(jù)你的問題,我了解到你想為每個(gè)元素提供 id 和獲取的數(shù)組中的值。
所以對(duì)于第一個(gè)<li>元素,id 將是category 1。如果這是您要的,解決方案是獲取所需的元素
<div id="parent">
<ul>
<li role="presentation" class="active categories"><a href="#all" aria-controls="all" role="tab" data-toggle="tab">ALL</a></li>
<li role="presentation" class="categories"><a href="#chicken" aria-controls="chicken" role="tab" data-toggle="tab">CHICKEN</a></li>
<li role="presentation" class="categories"><a href="#fish" aria-controls="fish" role="tab" data-toggle="tab">FISH</a></li>
<li role="presentation" class="categories"><a href="#turkey" aria-controls="turkey" role="tab" data-toggle="tab">TURKEY</a></li>
<li role="presentation" class="categories"><a href="#miscellenous" aria-controls="miscellenous" role="tab" data-toggle="tab">MISCELLANOUS</a></li>
<li role="presentation" class="categories"><a href="#frozen" aria-controls="last" role="tab" data-toggle="tab">LAST</a></li>
</ul>
</div>
let fetchedArray = ['cat1','cat2','cat3','cat4','cat5','cat6'];
$('li').each((i,el) => {
$(el).attr('id',fetchedArray[i]);
})

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊
`所以,根據(jù)你的問題,我了解到你想為每個(gè)元素提供 id 和獲取的數(shù)組中的值。
所以對(duì)于第一個(gè)<li>元素,id 將是category 1。如果這是您要的,解決方案是獲取所需的元素
<div id="parent">
<ul>
<li role="presentation" class="active categories"><a href="#all" aria-controls="all" role="tab" data-toggle="tab">ALL</a></li>
<li role="presentation" class="categories"><a href="#chicken" aria-controls="chicken" role="tab" data-toggle="tab">CHICKEN</a></li>
<li role="presentation" class="categories"><a href="#fish" aria-controls="fish" role="tab" data-toggle="tab">FISH</a></li>
<li role="presentation" class="categories"><a href="#turkey" aria-controls="turkey" role="tab" data-toggle="tab">TURKEY</a></li>
<li role="presentation" class="categories"><a href="#miscellenous" aria-controls="miscellenous" role="tab" data-toggle="tab">MISCELLANOUS</a></li>
<li role="presentation" class="categories"><a href="#frozen" aria-controls="last" role="tab" data-toggle="tab">LAST</a></li>
</ul>
</div>
let fetchedArray = ['cat1','cat2','cat3','cat4','cat5','cat6'];
$('li').each((i,el) => {
$(el).attr('id',fetchedArray[i]);
})

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果要更改 id,可以使用 for 循環(huán)遍歷元素并更改它們。
const categories = ["category 1", "category 2", "category 3", "category 4", "category 5", "category 6"];
for (var i = 0; i < document.getElementsByTagName("li").length; i++) {
let el = document.getElementsByTagName("li")[i];
el.id = categories[i];
console.log(el);
}
<li role="presentation" class="active categories"><a href="#all" aria-controls="all" role="tab" data-toggle="tab">ALL</a></li>
<li role="presentation" class="categories"><a href="#chicken" aria-controls="chicken" role="tab" data-toggle="tab">CHICKEN</a></li>
<li role="presentation" class="categories"><a href="#fish" aria-controls="fish" role="tab" data-toggle="tab">FISH</a></li>
<li role="presentation" class="categories"><a href="#turkey" aria-controls="turkey" role="tab" data-toggle="tab">TURKEY</a></li>
<li role="presentation" class="categories"><a href="#miscellenous" aria-controls="miscellenous" role="tab" data-toggle="tab">MISCELLANOUS</a></li>
<!--
<li role="presentation" class="categories">
<a href="#frozen" aria-
-->
上面的代碼遍歷了lis
并將 ids 附加到 中的元素categories
。然后它將元素打印到控制臺(tái),這樣您就可以看到它們的 id 是如何設(shè)置的。
添加回答
舉報(bào)