[英]jQuery Optimization - Dry
我有一個簡單的情況:
$("#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."
});
唯一的區別是占位符:“ ...”。
如何優化此代碼使其不重復(DRY)?
嘗試這個:
$("#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: ($(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.