我怎样才能尽量减少以下绑定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