Как изменить порядок тегов <a>в алфавитном порядке? - PullRequest
0 голосов
/ 06 мая 2019

У меня есть список ~ 1200 <a> тегов, которые должны быть организованы в алфавитном порядке.Можно ли перестроить этот список в алфавитном порядке на основе <a> Содержимое </a> внутри тега, сохраняя всю структуру ссылок?

пример:

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

1 Ответ

0 голосов
/ 06 мая 2019

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

...