简体   繁体   English

将 aria-labelledby 属性添加到 Jquery 数据表分页

[英]Add aria-labelledby attribute to Jquery Datatable Pagination

Here is the current HTML generated by the Jquery Datatable plug-in for pagination( https://datatables.net/plug-ins/pagination/full_numbers_no_ellipses ):下面是Jquery Datatable分页插件生成的当前HTML( https://datatables.net/plug-ins/pagination/full_numbers_no_ellipses ):

<div class="dataTables_paginate paging_full_numbers" id="my-table_paginate" aria-label="Pagination navigation">
    <ul class="pagination">
        <li class="paginate_button page-item first disabled" id="my-table_first" aria-label="Go to Page 1">
            <a href="#" aria-controls="my-table" data-dt-idx="0" tabindex="0" class="page-link">First</a>
        </li>
        <li class="paginate_button page-item previous disabled" id="my-table_previous" aria-label="Go to previous page">
            <a href="#" aria-controls="my-table" data-dt-idx="1" tabindex="0" class="page-link">Previous</a>
        </li>
        <li class="paginate_button page-item active" aria-current="page">
            <a href="#" aria-controls="my-table" data-dt-idx="2" tabindex="0" class="page-link">1</a>
        </li>
        <li class="paginate_button page-item ">
            <a href="#" aria-controls="MY-table" data-dt-idx="3" tabindex="0" class="page-link">2</a>
        </li>
        <li class="paginate_button page-item ">
            <a href="#" aria-controls="my-table" data-dt-idx="4" tabindex="0" class="page-link">3</a>
        </li>
        <li class="paginate_button page-item next" id="my-table_next" aria-label="Go to next page">
            <a href="#" aria-controls="my-table" data-dt-idx="5" tabindex="0" class="page-link">Next</a>
        </li>
        <li class="paginate_button page-item last" id="my-table_last" aria-label="Go to last page">
            <a href="#" aria-controls="my-table" data-dt-idx="6" tabindex="0" class="page-link">Last</a>
        </li>
    </ul>
</div>

I want to add aria-labelledby attributes for the individual page numbers 1,2,3.我想为单独的页码 1、2、3 添加 aria-labelledby 属性。 (eg. for page 1, aria-labelledby="Go to Page 1"). (例如,对于第 1 页,aria-labelledby="转到第 1 页")。 I added it to the first, next, previous, and last labels using Jquery as so:我使用 Jquery 将它添加到第一个、下一个、上一个和最后一个标签中,如下所示:

$("#my-table_paginate").attr("aria-label", "Pagination navigation")
$("#my-table_first").attr("aria-label", "Go to Page 1")
$("#my-table_previous").attr("aria-label", "Go to previous page")
$("#my-table_next").attr("aria-label", "Go to next page")
$("#my-table_last").attr("aria-label", "Go to last page")
$("#my-table_paginate .pagination .active").attr("aria-current", "page")

How do I do the same for individual pages since they all share the same class?由于它们都共享相同的 class,我该如何对各个页面执行相同的操作? I'm thinking of using a for loop to loop through the a tags in the ul and adding the aria attribute using jquery, but am not sure how to do so.我正在考虑使用 for 循环遍历 ul 中的 a 标签并使用 jquery 添加 aria 属性,但我不确定如何操作。

Try each loop for class name:为 class 名称尝试每个循环:

$('.paginate_button').each(function () {
    var pageNumber = $(this).text();
    // check if pageNumber is a number
    if (!isNaN(pageNumber)) {
        $(this).attr('aria-label', 'Go To Page ' + pageNumber);
    }
});

A somewhat brute-force approach is as follows:一种有点蛮力的方法如下:

  let table = $('#my-table').DataTable( {
    initComplete: function(settings, json) {
      addAriaClasses();
    }
  } );

  $('#my-table').on( 'draw.dt', function () {
    addAriaClasses();
  } );

  function addAriaClasses() {
    $('.paginate_button').each(function () {
      $(this).attr("aria-label", "Go to page " + $(this).text());
    });
    $("#my-table_paginate").attr("aria-label", "Pagination navigation")
    $("#my-table_first").attr("aria-label", "Go to Page 1")
    $("#my-table_previous").attr("aria-label", "Go to previous page")
    $("#my-table_next").attr("aria-label", "Go to next page")
    $("#my-table_last").attr("aria-label", "Go to last page")
    $("#my-table_paginate .pagination .active").attr("aria-current", "page")
  }

} );

This assumes the table is defined with an ID of my-table :这假设表是用my-table的 ID 定义的:

<table id="my-table" ... >

It uses the DataTables initComplete function to handle the initial draw of the table, and then uses the DataTables draw event to handle subsequent re-draws.它使用DataTables initComplete function 来处理表格的初始绘制,然后使用DataTables 绘制事件来处理后续的重新绘制。

I say this is a "brute-force" approach because it first assumes all buttons show page numbers, and then overwrites those special cases which are not numbers, using the code you already provided in the question.我说这是一种“蛮力”方法,因为它首先假定所有按钮都显示页码,然后使用您在问题中已经提供的代码覆盖那些不是数字的特殊情况。 You could probably use a switch statement to streamline this.您或许可以使用switch语句来简化此操作。

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

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