首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何按字母顺序重新排序<a>标记列表?

如何按字母顺序重新排序<a>标记列表?
EN

Stack Overflow用户
提问于 2019-05-06 20:05:57
回答 1查看 30关注 0票数 0

我有一个大约1200个<a>标签的列表,需要按字母顺序组织。是否可以在保留所有链接结构的同时,根据标记内的<a>内容</a>按字母顺序重新排列此列表?

示例:

代码语言:javascript
复制
<a href="https://digicoll.library.utoronto.ca/mmdl/UPT106F.pdf">
Vijayakārī-[jātaka-tō] (sections 4–9)</a>

<a href="https://digicoll.library.utoronto.ca/mmdl/UPT002F.pdf">Sarabhaṅga-pyui.</a>

<a href="https://digicoll.library.utoronto.ca/mmdl/UPT108F.pdf">Saṅkhārabhājanī</a>

<a href="https://digicoll.library.utoronto.ca/mmdl/UPT109F.pdf">Jhāpanaku suil-achuṃ:-aphrat</a>

<a href="https://digicoll.library.utoronto.ca/mmdl/UPT110F.pdf">[Mahā]sutasoma-pyui.</a>
EN

回答 1

Stack Overflow用户

发布于 2019-05-06 20:45:23

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<title>Sort a HTML List Alphabetically</title>
<body>

<p>Click the button to sort the list alphabetically:</p>
<button onclick="sortList()">Sort</button>

<ul id="id01">
  <li>Oslo</li>
  <li>Stockholm</li>
  <li>Helsinki</li>
  <li>Berlin</li>
  <li>Rome</li>
  <li>Madrid</li>
</ul>

<script>
function sortList() {
  var list, i, switching, b, shouldSwitch;
  list = document.getElementById("id01");
  switching = true;
  /* Make a loop that will continue until
  no switching has been done: */
  while (switching) {
    // start by saying: no switching is done:
    switching = false;
    b = list.getElementsByTagName("LI");
    // Loop through all list-items:
    for (i = 0; i < (b.length - 1); i++) {
      // start by saying there should be no switching:
      shouldSwitch = false;
      /* check if the next item should
      switch place with the current item: */
      if (b[i].innerHTML.toLowerCase() > b[i + 1].innerHTML.toLowerCase()) {
        /* if next item is alphabetically
        lower than current item, mark as a switch
        and break the loop: */
        shouldSwitch = true;
        break;
      }
    }
    if (shouldSwitch) {
      /* If a switch has been marked, make the switch
      and mark the switch as done: */
      b[i].parentNode.insertBefore(b[i + 1], b[i]);
      switching = true;
    }
  }
}
</script>

</body>
</html>

来自-> https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sort_list的源代码您只需将'LI‘替换为'a’

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56004869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档