我怎樣才能儘量減少以下綁定jQuery中:如何最小化jquery中的重複代碼?
var profileIdDefault = "Profile ID";
var organisationIdDefault = "Competitor ID";
var FromDateDefault = "From Date";
var ToDateDefault = "To Date";
$("#startDate").focus(function() {
if ($(this).val() === FromDateDefault) {
$(this).attr("value", "");
}
});
$("#startDate").blur(function() {
if ($(this).val() === '') {
$(this).val(FromDateDefault);
}
});
$("#endDate").focus(function() {
if ($(this).val() === ToDateDefault) {
$(this).attr("value", "");
}
});
$("#endDate").blur(function() {
if ($(this).val() === '') {
$(this).val(ToDateDefault);
}
});
$("#profileID").focus(function() {
if ($(this).val() === profileIdDefault) {
$(this).attr("value", "");
}
});
$("#profileID").blur(function() {
if ($(this).val() === '') {
$(this).val(profileIdDefault);
}
});
$("#organisationID").focus(function() {
if ($(this).val() === organisationIdDefault) {
$(this).attr("value", "");
}
});
$("#organisationID").blur(function() {
if ($(this).val() === '') {
$(this).val(organisationIdDefault);
}
});
什麼是你的HTML標記是什麼樣子? – George 2013-04-25 08:36:44
應該在代碼評論中發佈 – lifetimes 2013-04-25 08:36:58
而你真正要問的是「如何重構這個jQuery代碼?」 – 2013-04-25 08:53:16