问题
I'm looking to create an isotope filter to a div list and as such want to add a class of the year to a parent container/div by extracting the 'Year' from a displayed date and then adding that as the class to the parent div (.publication
)
Right now I am using a very basic method to do this by looking to see if a div contains a specific year and then individually assigning a year:
HTML
<div>
<div class="publication addyearclasshere">
<div class="publication-date">15/3/2017</div>
</div>
<div class="publication addyearclasshere">
<div class="publication-date">15/3/2016</div>
</div>
Code
jQuery('div[class*="publication-date"]:contains("2020")').closest('.publication').addClass('2020');
jQuery('div[class*="publication-date"]:contains("2019")').closest('.publication').addClass('2019');
jQuery('div[class*="publication-date"]:contains("2018")').closest('.publication').addClass('2018');
jQuery('div[class*="publication-date"]:contains("2017")').closest('.publication').addClass('2017');
jQuery('div[class*="publication-date"]:contains("2016")').closest('.publication').addClass('2016');
jQuery('div[class*="publication-date"]:contains("2015")').closest('.publication').addClass('2015');
The problem with this approach is that if we go below 2015 or above 2020 no class is assigned. If we can explode the date and extract the year for the class then this would keep going year on year?
回答1:
Loop through each .publication-date
and extract the year from its text. Add the year as a class to its parent. Like this:
jQuery(".publication-date").each(function(i) {
var yr = $(this).text().trim().split('/')[2];
jQuery(this).closest('.publication').addClass(yr);
});
Working Fiddle
EDIT: To answer the OPs question in the comments:
Use replace
to replace all the spaces by '-' and add the result to .publication
's class.
jQuery(".publication-name").each(function(i) {
var name = $(this).text().trim().replace(' ', '-');
jQuery(this).closest('.publication').addClass(name);
});
回答2:
You can loop through each of the elements and split the text.
And you don't need to use .closest()
but just use .parent()
Updated. Since your code in the example in the one on your website. you would have to use closest to match the website code. Updated html to match your website html.
jQuery('div[class*="publication-date"]').each(function() {
var year = $(this).text().split("/");
$(this).closest(".publication").addClass(year[year.length - 1]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<div class="publication w-col w-col-6 w-dyn-item">
<div class="publication-row w-row">
<div class="w-col w-col-10">
<div>
<h1>Cosmética</h1>
</div>
<div class="publication-meta-row w-row">
<div class="w-col w-col-9">
<div class="publication-meta-row w-row">
<div class="w-col w-col-3">
<div class="avatar" style="background-image: url('https://daks2k3a4ib2z.cloudfront.net/58cbe3b491c05a4f18aa9e82/58dd000bd8b55b726fac170e_NSS_L1600745.png');">
<div class="avatar-title w-embed">
<div title="Nielson Sánchez Stewart " style="display: block; width; 100%; height: 100%; position: relative; top: 0; right; 0;"></div>
</div>
</div>
</div>
<div class="w-col w-col-9">
<div class="publication-name">Sur</div>
</div>
</div>
</div>
<div class="w-col w-col-3">
<div class="publication-date">15/3/2017</div>
</div>
</div>
</div>
<div class="w-col w-col-2">
<div class="download-wrapper">
<div class="pdf w-embed"><a href="https://www.dropbox.com/s/vsh8wc5bktgmps7/Art%C3%ADculo%202017.03.15%20-%20Cosm%C3%A9tica.pdf?dl=0" target="_blank" title="Download/Preview in Dropbox"><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a></div>
</div>
</div>
</div>
</div>
</div>
来源:https://stackoverflow.com/questions/44268592/jquery-extract-year-from-date-and-add-class-to-parent