Stop值在javascript中变为负数

问题描述 投票:0回答:1

我具有一个功能,您可以单击以点赞,也可以单击以不喜欢。如何使不可能变为负数,以便如果在0之后单击“不喜欢”,则保持为0?

function stem(id) {
    var antal_stemmer = document.getElementById(id).className; 
    var nyt_antal_stemmer =+ antal_stemmer + +1; 
    document.getElementById(id).className = nyt_antal_stemmer;
    var indhold = document.getElementsByClassName(id); 
    var indholdA = indhold[0].innerHTML; 
    indhold[0].innerHTML = nyt_antal_stemmer + " stemmer"; 
}
function fjern(id) {
    var antal_stemmer = document.getElementById(id).className;
    var nyt_antal_stemmer =+ antal_stemmer - +1;
    document.getElementById(id).className = nyt_antal_stemmer;
    var indhold = document.getElementsByClassName(id);
    var indholdA = indhold[0].innerHTML;
    indhold[0].innerHTML = nyt_antal_stemmer + " stemmer";
}

javascript count negative-number
1个回答
1
投票

您可以获得最大值或零。结果是一个大于或等于零的数字。

var nyt_antal_stemmer = Math.max(0, +antal_stemmer - 1); // with unary + for a string
© www.soinside.com 2019 - 2024. All rights reserved.