jQuery函数避免复制/粘贴

时间:2017-09-06 09:41:14

标签: javascript jquery

我有一个jQuery可以按年加载div个内容(从2008年到当年)。

这是我的代码:

$('.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');
});

所以我每年都会复制/粘贴我的第一个.click函数。

修改

这是生成的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>
   [...]

来自DOM的HTML输出仅用于项目:

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

是否可以使用循环或其他内容来避免复制/粘贴并返回current year。 问题是每年都要复制/粘贴新的一年。

4 个答案:

答案 0 :(得分:1)

只需创建一个从特定年份开始并在某一年结束的循环。并将年份插入模板中,如下所示:

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');
    });
}

你可以用数组做同样的事情:

var years = [2000, 2008, 2010]
years.forEach(function(year) {
    // same template as above in here
})

答案 1 :(得分:1)

如果您要使用idclass点击div,可以尝试:

$('.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();允许您恢复日期。

答案 2 :(得分:0)

&#13;
&#13;
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');
  });
}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

如果可能,您可以为包含唯一年份标识符的每个相关元素添加数据属性或类似内容,例如:data-filtre-year="2008" - 然后使用该数据属性作为选择器,您可以绑定事件。

$('.filtre [data-filtre-year]').click(function() {
    var $currentElement = $(this);
    var anneeClass = '.annee' + $currentElement.data('filtreYear');

    $('.loader').show().delay(7000).fadeOut();
    $('.selected .homeliVa li').hide('slow');
    $('.homeliDp .itemCom').hide('slow');
    $('.selected .homeliAlm li').hide('slow');
    setTimeout('$(".selected ' + anneeClass + '").show()', 5000);
    $('.selected .nothing').hide('slow');
    $('.filtre .date').removeClass('selectedF');

    $currentElement.addClass('selectedF'); // as you are already in the context of the element you can use "this"

    $('.selected .itemCom').removeClass('selectedItem');
    $('.selected ' + anneeClass).addClass('selectedItem');
});