jQuery function to avoid copy/paste
I have a jQuery to load div
content by year (from 2008 to current year).
Here is my code :
$('.filtre .f2017').click(function() {
$('.loader').show().delay(7000).fadeOut();
$('.selected .homeliVa li').hide('slow');
$('.homeliDp .itemCom').hide('slow');
$('.selected .homeliAlm li').hide('slow');
setTimeout('$(".selected .annee2017").show()', 5000);
$('.selected .nothing').show('slow');
$('.loader').show().delay(7000).fadeOut();
$('.filtre .date').removeClass('selectedF');
$('.filtre .f2017').addClass('selectedF');
$('.selected .itemCom').removeClass('selectedItem');
$('.selected .annee2017').addClass('selectedItem');
});
[...]
$('.filtre .f2008').click(function() {
$('.loader').show().delay(7000).fadeOut();
$('.selected .homeliVa li').hide('slow');
$('.homeliDp .itemCom').hide('slow');
$('.selected .homeliAlm li').hide('slow');
setTimeout('$(".selected .annee2008").show()', 5000);
$('.selected .nothing').hide('slow');
$('.filtre .date').removeClass('selectedF');
$('.filtre .f2008').addClass('selectedF');
$('.selected .itemCom').removeClass('selectedItem');
$('.selected .annee2008').addClass('selectedItem');
});
So I copy/paste my first .click
function for every year.
EDIT
Here is generated HTML :
$args = array(
'posts_per_page' => '-1',
'orderby' => 'post_date',
'post_type'=> 'dp',
'order' => 'DESC',
'post_status' => 'publish'
);
while( $the_query->have_posts() ) {
[...]
<a href="<?php echo esc_url( $post->guid ); ?>" title="<?php echo esc_attr( get_the_title() ); ?>"><?php echo get_the_post_thumbnail(); ?></a>
<h3>
<a href="<?php echo esc_url( $post->guid ); ?>" title="<?php echo esc_attr( get_the_title() ); ?>">
<?php echo esc_html( get_the_title() ); ?>
</a>
</h3>
[...]
And HTML output from DOM for only une item :
<div id="post-16351" class="itemCom annee2014 selectedItem" style="display: block;">
<div class="postHeader">
<h3>
<a href="url" title="title">
Post title
</a>
</h3>
<div class="postMeta">
<ul class="styles">
<li class="term_id">
<a href="url" rel="tag">Term name</a>
</li>
</ul>
<span>Publié le : 17 décembre 2014</span>
</div>
</div>
<div class="fichiersDp">
<a class="download-link filetype-icon fichier-pdf" target="blank_" href="url">name_of_file_attachment</a>
</div>
</div>
Is it possible to have a loop or something else to avoid the copy/paste and return current year
. Problem is that every year, I should copy/paste for new year.
If you have div to click with id
or class
you can try :
$('.filtre .yourClass').click(function() {
var item = $( this ).find('.class').text(); //If the text "button" is `2017` for example, this will return `2017`
$('.loader').show().delay(7000).fadeOut();
$('.selected .homeliVa li').hide('slow');
$('.homeliDp .itemCom').hide('slow');
$('.selected .homeliAlm li').hide('slow');
setTimeout($(".selected .annee" + item).show(), 5000);
$('.selected .nothing').hide('slow');
$('.filtre .date').removeClass('selectedF');
$('.filtre .f' + item).addClass('selectedF');
$('.selected .itemCom').removeClass('selectedItem');
$('.selected .annee' + item).addClass('selectedItem');
});
var item = $( this ).find('element or .div').text();
allow you to recover the date.
Just make a loop that starts at a certain year and finishes at a certain year. And insert the year into the template like so:
for (var year = 2000; year < 2008; year++) {
$('.filtre .f' + year).click(function() {
$('.loader').show().delay(7000).fadeOut();
$('.selected .homeliVa li').hide('slow');
$('.homeliDp .itemCom').hide('slow');
$('.selected .homeliAlm li').hide('slow');
setTimeout('$(".selected .annee' + year + '").show()', 5000);
$('.selected .nothing').show('slow');
$('.loader').show().delay(7000).fadeOut();
$('.filtre .date').removeClass('selectedF');
$('.filtre .f' + year).addClass('selectedF');
$('.selected .itemCom').removeClass('selectedItem');
$('.selected .annee' + year).addClass('selectedItem');
});
}
You could do the same thing with an array:
var years = [2000, 2008, 2010]
years.forEach(function(year) {
// same template as above in here
})
var years = [2017, 2008] //put all needed years in array
for (var i = 0; i < years.length; i++) {
$('.filtre .f' + years[i]).click(function() {
$('.loader').show().delay(7000).fadeOut();
$('.selected .homeliVa li').hide('slow');
$('.homeliDp .itemCom').hide('slow');
$('.selected .homeliAlm li').hide('slow');
setTimeout('$(".selected .annee" + years[i]).show()', 5000);
$('.selected .nothing').hide('slow');
$('.filtre .date').removeClass('selectedF');
$('.filtre .f' + years[i]).addClass('selectedF');
$('.selected .itemCom').removeClass('selectedItem');
$('.selected .annee' + years[i]).addClass('selectedItem');
});
}
链接地址: http://www.djcxy.com/p/83462.html
上一篇: 倍数复选框相同.click功能
下一篇: jQuery函数避免复制/粘贴