[英]How can I write this function without using a modulo?
I'm wondering if there is another way to write this function without using a modulo. 我想知道是否还有另一种无需使用模数即可编写此函数的方法。 I realized that I have another piece of code that requires me to click the #mail-wrap button and doing so messes up the number of clicks which affects this function. 我意识到我还有另一段代码,要求我单击#mail-wrap按钮,这样做会使影响此功能的单击次数混乱。
It's just a simple switch. 这只是一个简单的开关。 I'm not too good with conditionals. 我对条件句不太满意。
$('#mail-wrap').click(function (e) {
e.preventDefault();
var c = 0;
if (c++ % 2 == 0) {
$('#contact-button').addClass('project-button').text('Projects');
} else {
$('#contact-button').removeClass('project-button').text('Get in touch');
}
});
Edit: Changed the question a bit. 编辑:改变了一点问题。 Sorry, the last one was too broad. 抱歉,最后一个范围太广。
As Boldewyn mentioned, most likely your problem is that you are defining a global variable c
. 正如Boldewyn所述,您最有可能遇到的问题是正在定义全局变量c
。 But if you would like to avoid this variable completely you could check for the CSS-class of contact-button
via the jQuery hasClass
function, ie 但是,如果您想完全避免使用此变量,则可以通过jQuery hasClass
函数检查contact-button
的CSS类,即
$('#mail-wrap').click(function (e) {
...
var contactButton = $('#contact-button');
if (!contactButton.hasClass('project-button')) {
$('#contact-button').addClass('project-button').css('width', '71px').text('Projects');
...
} else {
$('#contact-button').removeClass('project-button').css('width', '96px').text('Get in touch');
...
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.