实际上我不知道如何问这个问题,我有一个对象存储数据库中的图像路径,它在我的页面上显示为一个类别,每个 Angular 色有 4 个图像。
如果对象长度是偶数(例如 4),看起来很棒,但如果是奇数,则看起来很奇怪,因此我决定在对象长度为奇数时向其添加一个空图像框。
但是我该如何开始呢?我不知道,像这样:
box = 7 now (which is a odd);
var checkCategoryNumber = function (box){
if (box.length % 2 == 0) {
// do noting
alert("even");
}
else {
// increase and create an empty image box, till the number is even.
alert("odd");
}
}
这将创建一个空图像框到 8,然后停止。
希望您明白我的意思,如果问题令人困惑,请告诉我,谢谢。
最佳答案
您可以检查box
是否为奇数以及是否添加空框。
var checkCategoryNumber = function(box) {
for (var i = 0; i < box; i++) {
$('body').append(i + 1 + '. Box <br>')
}
if (box % 2 != 0) $('body').append(box + 1 + '. Empty box <br><hr>')
}
checkCategoryNumber(7)
checkCategoryNumber(6)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
关于javascript - 增加并创建空图像框,直到数量为偶数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41361783/