重复我的jQuery中的函数,有更好的方法吗
Repeating functions in my jQuery, is there a better way to do this?
我正在摆弄一些jQuery,并通过构建自己版本的Github Pages教程来自学一些基础知识。我发现我经常重复我的功能,以便在不同的地方实现类似的功能。
// USER / ORGANISATION SITE
// ----------------------- //
// User clicks on #userSite
$("#js-userSite").click( function() {
// Check if body has other class, if so remove it
if( $("body").hasClass("showProjectSite") ) {
$("body").removeClass("showProjectSite");
}
// And an appropriate class is added to the body
$("body").addClass("showUserSite");
});
// USER / ORGANISATION SITE ----> CHOOSE GIT CLIENT
// ----------------------------------------------- //
// Click event
$("#js-gitClientTerminal").click( function() {
// Check if body has other classes, if so remove it
if( $("body").hasClass("showGitClientMac") ) {
$("body").removeClass("showGitClientMac");
}
else if ( $("body").hasClass("showGitClientWindows") ) {
$("body").removeClass("showGitClientWindows");
}
// And an appropriate class is added to the body
$("body").addClass("showGitClientTerminal");
});
// User clicks on #projectSite
$("#js-gitClientWindows").click( function() {
// Check if body has other class, if so remove it
if( $("body").hasClass("showGitClientMac") ) {
$("body").removeClass("showGitClientMac");
}
else if ( $("body").hasClass("showGitClientTerminal") ) {
$("body").removeClass("showGitClientTerminal");
}
// And an appropriate class is added to the body
$("body").addClass("showGitClientWindows");
});
// User clicks on #projectSite
$("#js-gitClientMac").click( function() {
// Check if body has other class, if so remove it
if( $("body").hasClass("showGitClientTerminal") ) {
$("body").removeClass("showGitClientTerminal");
}
else if ( $("body").hasClass("showGitClientWindows") ) {
$("body").removeClass("showGitClientWindows");
}
// And an appropriate class is added to the body
$("body").addClass("showGitClientMac");
});
// PROJECT SITE
// ----------- //
// User clicks on #projectSite
$("#js-projectSite").click( function() {
// Check if body has other class, if so remove it
if( $("body").hasClass("showUserSite") ) {
$("body").removeClass("showUserSite");
} else if ( $("body").hasClass("showGitClientMac") ) {
("body").removeClass("showGitClientMac");
} else if ( $("body").hasClass("showGitClientWindows") ) {
("body").removeClass("showGitClientWindows");
} else if ( $("body").hasClass("showGitClientTerminal") ) {
("body").removeClass("showGitClientTerminal");
}
// And an appropriate class is added to the body
$("body").addClass("showProjectSite");
});
// PROJECT SITE ----> GENERATE OR BUILD FROM SCRATCH
// ------------------------------------------------- //
// User clicks on #projectSite
$("#js-generateSite").click( function() {
// Check if body has other class, if so remove it
if( $("body").hasClass("showStartFromScratch") ) {
$("body").removeClass("showStartFromScratch");
}
// And an appropriate class is added to the body
$("body").addClass("showGenerateSite");
});
// User clicks on #projectSite
$("#js-startFromScratch").click( function() {
// Check if body has other class, if so remove it
if( $("body").hasClass("showGenerateSite") ) {
$("body").removeClass("showGenerateSite");
}
// And an appropriate class is added to the body
$("body").addClass("showStartFromScratch");
});
我知道有一种更精简、更干净的方法可以做到这一点。有人能给我指正确的方向吗?
您可以创建这样一个简单的函数:
function checkIfBodyHasClassIfSoRemoveIt(className, classNameAlt){
if($("body").hasClass(className)) {
$("body").removeClass(className);
} else if (classNameAlt && $("body").hasClass(classNameAlt)) {
$("body").removeClass(classNameAlt);
}
}
你可以这样使用:
// User clicks on #userSite
$("#js-userSite").click( function() {
// Check if body has other class, if so remove it
checkIfBodyHasClassIfSoRemoveIt("showProjectSite");
// And an appropriate class is added to the body
$("body").addClass("showUserSite");
});
// USER / ORGANISATION SITE ----> CHOOSE GIT CLIENT
// ----------------------------------------------- //
// Click event
$("#js-gitClientTerminal").click( function() {
// Check if body has other classes, if so remove it
checkIfBodyHasClassIfSoRemoveIt("showGitClientMac", "showGitClientWindows");
// And an appropriate class is added to the body
$("body").addClass("showGitClientTerminal");
});
....
....
....
我还没有测试过这个代码,但应该可以工作!
相关文章:
- 在JavaScript中拆分日期字符串的更好方法是什么
- 设置嵌套对象属性的更好方法
- 用Javascript重新格式化复杂文本日期字符串的更好方法
- 什么'这是从第三个函数上的async 1st函数获得结果的更好方法
- 将数组从javascript格式化为php的更好方法,反之亦然
- jquery:将动画绑定到滚动条位置的更好方法
- 使用较少代码隐藏和显示选择菜单内容的更好方法是什么?javascript
- 编写if-else语句的更好方法
- 传递promise回调方法的更好方法
- 在Knockoutjs中设置计算对象的observableArray的更好方法
- 从网页获取数据的更好方法,而不是使用DOM和HTML元素
- Backbone js代码气味-嵌入子视图的更好方法
- 从数组中删除项的更好方法
- 对多维数组进行分组的更好方法
- 在node.js和express中基于路径运行不同数据库查询的更好方法
- 动态更改测试中代码覆盖率的require语句的更好方法
- 设计具有数据持久性的web应用程序的更好方法
- 选择表行的更好方法
- 在nodejs服务器上提供文件的更好方法,而不是if/else
- Javascript;Ajax——填充对象的更好方法