这段代码有效,但我确信有一种更有效的方法可以使用我的新手级别以上的人可能会提供帮助的功能来完成这项工作。
我正在根据类型的 div 计数更改按钮标签文本字符串。如果计数为零,则隐藏按钮。如果计数等于 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();
};
在此先感谢您的帮助!仍然在学习。