Combine 2 Functions to Write to an Attribute Array in jQuery or Javascript

后端 未结 1 1693
[愿得一人]
[愿得一人] 2021-01-15 23:18

This is an extension of original question: jQuery Extract Year from Date and Add Class to Parent

... but I need to extend it so that the 2 existing functions are com

1条回答
  •  走了就别回头了
    2021-01-15 23:29

    Assuming that, in your HTML, each of your .publication div has one .publicaton-date div and one publication-name div, you can do something on the lines of:

    $('.publication').each(function() {
        var yr = $(this).find('.publication-date').text().trim().split('/')[2];
        var name = $(this).find('.publication-name').text().trim().replace(/[_\W]+/g, "-");
        $(this).attr('data-filter', yr + ',' + name);
    })
    

    ie: Instead of looping through the .publication-date and .publication-name elements, loop through the .publication elements and find the date and year inside them.

    0 讨论(0)
提交回复
热议问题