简体   繁体   English

jQuery优化-干

[英]jQuery Optimization - Dry

I've this simple situation: 我有一个简单的情况:

$("#check-in").dateDropper({
    years_multiple: "10",
    format: "d-m-Y",
    minYear: "2015",
    maxYear: "2016",
    lang: "pt",
    animation: "bounce",
    placeholder: "Dia da entrada."
});

$("#check-out").dateDropper({
    years_multiple: "10",
    format: "d-m-Y",
    minYear: "2015",
    maxYear: "2016",
    lang: "pt",
    animation: "bounce",
    placeholder: "Dia da saída."
});

The only difference is placeholder: "...". 唯一的区别是占位符:“ ...”。

How can I optimize this code to not be repetitive (DRY)? 如何优化此代码使其不重复(DRY)?

Try this: 尝试这个:

$("#check-in, #check-out").dateDropper({
    years_multiple: "10",
    format: "d-m-Y",
    minYear: "2015",
    maxYear: "2016",
    lang: "pt",
    animation: "bounce",
    placeholder:($(this).attr('id') == "check-in" ? "Dia da entrada." : "Dia da saída.")
});

Try This :- 尝试这个 :-

$("#check-in,#check-out").dateDropper({
    years_multiple: "10",
    format: "d-m-Y",
    minYear: "2015",
    maxYear: "2016",
    lang: "pt",
    animation: "bounce",
    placeholder: ($(this).attr('id') == "check-in" ? "Dia da entrada." : "Dia da saída.")
});
$("#check-in,#check-out").dateDropper({
    years_multiple: "10",
    format: "d-m-Y",
    minYear: "2015",
    maxYear: "2016",
    lang: "pt",
    animation: "bounce",
    placeholder: "Dia da "+($(this).attr('id') == "check-in" ? "entrada." : "saída.")
});
$("#check-in,#check-out").each(function(){
    var daylabel = this.id==="check-in" ? "entrada" : "saida";
    $(this).dateDropper({
        years_multiple: "10",
        format: "d-m-Y",
        minYear: "2015",
        maxYear: "2016",
        lang: "pt",
        animation: "bounce",
        placeholder: "Dia da "+daylabel+"."
    });
});

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

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