简体   繁体   English

如何使用 id 的数组动态更改 href 属性?

[英]how to dynamically change the href attribute with the array of id's?

I have these elements and I want to change the id's dynamically using the fetched id array via server which I have categories (Ex: ["category 1", "category 2","category 3","category 4","category 5", "category 6"] ).我有这些元素,我想通过我有类别的服务器使用获取的 id 数组动态更改 id(例如: ["category 1", "category 2","category 3","category 4","category 5", "category 6"] )。 I don't know how to do this nothing seems to work.我不知道该怎么做似乎没有任何工作。

<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-

If you want to change ids, you can use a for loop to iterate through the elements and change them.如果要更改 id,可以使用 for 循环遍历元素并更改它们。

 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- -->

The above iterates through the lis and attaches the ids to the elements in categories .上面遍历lis并将 id 附加到categories中的元素。 Then it prints the element to the console so you can see how their ids have been set.然后它将元素打印到控制台,这样您就可以看到它们的 id 是如何设置的。

`So, from your question, I understand that you want to give each of the elements the id with the value in the fetched array. `所以,从你的问题来看,我知道你想给每个元素一个 id 和获取的数组中的值。

So for the first <li> element, the id would be category 1 .所以对于第一个<li>元素,id 是category 1 If this is what you are asking for, the solution is to grab the required elements如果这是您所要求的,则解决方案是获取所需的元素

<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]);
    })

I added new class to the elements and then changed the href this way我向元素添加了新的 class ,然后以这种方式更改了 href

<a class="category-text" href="#chicken" aria-controls="chicken" role="tab" data-toggle="tab">CHICKEN</a>
var new_id = [];var category_name =[];
for(i in categories){
    new_id[i] = categories[i].id;
    category_name[i] = categories[i].name;
}

var i = 0;var new_name;
$(".category-text").each(function(i){
    new_name = '#' + new_id[i];
    $(this).attr('href', new_name);
    $(this).text(category_name[i]);
    i=i+1;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM