這段代碼很有效,但我確定有一個更高效的方法來完成這個功能,這個功能在我的新手級別上面可能有幫助。尋找比這個IF,ELSEIF,ELSE方法更好的解決方案
我正在改變按類型的div計數的按鈕標籤文本字符串。如果計數爲零,則按鈕被隱藏。如果計數等於1,則該術語是單數。如果計數大於1,則該術語是複數。
var countAll = $('li.posts').size();
var countText = $('div.text').size();
var countPhoto = $('div.photo').size();
var countQuote = $('div.quote').size();
var countLink = $('div.link').size();
var countChat = $('div.chat').size();
var countAudio = $('div.audio').size();
var countVideo = $('div.video').size();
var countAnswer = $('div.answer').size();
var countConversation = $('div.conversation').size();
$('.showAll').html("show all " + countAll + " posts ");
if (countText == 1) {
$('.showText').html(countText + " text");
} else if (countText > 1) {
$('.showText').html(countText + " texts");
} else {
$('.showText').hide();
};
if (countPhoto == 1) {
$('.showPhoto').html(countPhoto + " photo");
} else if (countPhoto > 1) {
$('.showPhoto').html(countPhoto + " photos");
} else {
$('.showPhoto').hide();
};
if (countQuote == 1) {
$('.showQuote').html(countQuote + " quote");
} else if (countQuote > 1) {
$('.showQuote').html(countQuote + " quotes");
} else {
$('.showQuote').hide();
};
if (countLink == 1) {
$('.showLink').html(countLink + " link");
} else if (countLink > 1) {
$('.showLink').html(countLink + " links");
} else {
$('.showLink').hide();
};
if (countChat == 1) {
$('.showChat').html(countChat + " chat");
} else if (countChat > 1) {
$('.showChat').html(countChat + " chats");
} else {
$('.showChat').hide();
};
if (countAudio == 1) {
$('.showAudio').html(countAudio + " audio");
} else if (countAudio > 1) {
$('.showAudio').html(countAudio + " audios");
} else {
$('.showAudio').hide();
};
if (countVideo == 1) {
$('.showVideo').html(countVideo + " video");
} else if (countVideo > 1) {
$('.showVideo').html(countVideo + " videos");
} else {
$('.showVideo').hide();
};
if (countAnswer == 1) {
$('.showAnswer').html(countAnswer + " answer");
} else if (countAnswer > 1) {
$('.showAnswer').html(countAnswer + " answers");
} else {
$('.showAnswer').hide();
};
if (countConversation == 1) {
$('.showConversation').html(countConversation + " conversation");
} else if (countConversation > 1) {
$('.showConversation').html(countConversation + " conversations");
} else {
$('.showConversation').hide();
};
在此先感謝您的幫助!仍在學習。
它應該屬於http://codereview.stackexchange。com – thecodeparadox
也許在[Code Review](http://codereview.stackexchange.com/)上問這個問題? – Sumo
對不起,不知道那個地區。 – user1452893