如何修复函数中的标签生成器循环?

问题描述

我是JS的新手,现在我想编写可为我的博客生成标签函数

    function generateTags(){
    
      /* find all articles */
    
      const articles = document.querySelectorAll('article');
      // console.log(articles);
    
      /* START LOOP: for every article: */
    
      let html = '';
    
      for(let article of articles){
    
        /* find tags wrapper */
    
        const tagWrapper = article.querySelector(optArticleTagsSelector);
        
        /* make html variable with empty string */
    
        /* get tags from data-tags attribute */
    
        const articleTags = article.getAttribute('data-tags');
    
        /* split tags into array */
    
        const articleTagsArray = articleTags.split(' ');
        // console.log(articleTagsArray);
       
        /* START LOOP: for each tag */
        for(let tag of articleTagsArray){
    
          console.log(tag);
    
          /* generate HTML of the link */
    
          const linkHTML = '<li><a href="#tag-' + tag +  '"><span>' + tag + '</span></a></li>';
    
          console.log(linkHTML);
          
          /* add generated code to html variable */
    
          html = html + linkHTML;
    
          /* END LOOP: for each tag */
    
        }
    
        /* insert HTML of all the links into the tags wrapper */
    
        tagWrapper.innerHTML = html;
    
        /* END LOOP: for every article: */     
    
      }
    
       
    }

generateTags();

博客中有10篇带有不同标签的不同文章。问题在于函数总结了上一篇文章中的标签,它看起来像:

第1条

lorem ipsum ...

标签:一二三

第2条

lorem ipsum

标签:一二三为五六

第3条

lorem ipsum

标签:一二三为五六七八九

等等。

预期结果:

第1条

lorem ipsum ...

标签:一二三

第2条

lorem ipsum

标签:五个五个

第3条

lorem ipsum

标签:七八九

我该如何解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)