javascript如果小于,显示销售

Jc0*_*807 0 html javascript

我能够设置一个脚本来显示"售罄"标签,如果该项目为0,如果该项目有1000显示"售完",这工作正常.

我想知道如果项目小于 1000而不是1000,我如何设置标签以显示"卖出"

通过在数字1000的前面放一个"<",我只是用它来飞扬它.

<script type="text/javascript">
jq(function() { 
    jq("span.spn_U3").each(function() { 
        switch(jq(this).text()) { 
            case "0": 
                jq(this).closest(".stylesummarytext").prev()
                    .append('<div class="styleoverlay soldout"><span>Sold Out</span></div>'); 
                break; 
            case "<1000": 
                jq(this).closest(".stylesummarytext").prev()
                    .append('<div class="styleoverlay sellingout"><span>Selling Out</span></div>'); 
                break;  
        } 
    });
});
</script>
Run Code Online (Sandbox Code Playgroud)

提前致谢

mea*_*gar 6

不要使用switch声明.使用if/else:

qty = parseInt(jq(this).text(), 10);

if (qty == 0) {
  jq(this).closest(".stylesummarytext").prev()
    .append('<div class="styleoverlay soldout"><span>Sold Out</span></div>');  
} else if (qty < 1000) {
  jq(this).closest(".stylesummarytext").prev()
    .append('<div class="styleoverlay sellingout"><span>Selling Out</span></div>'); 
} 
Run Code Online (Sandbox Code Playgroud)

请注意,您可以使用switch包含复杂案例的语句,但对于这样一组简单的案例,没有令人信服的理由:

qty = 0;

switch(true) {
case qty == 0:
  jq(this).closest(".stylesummarytext").prev()
    .append('<div class="styleoverlay soldout"><span>Sold Out</span></div>');  
  break;
case qty < 1000:
  jq(this).closest(".stylesummarytext").prev()
    .append('<div class="styleoverlay sellingout"><span>Selling Out</span></div>'); 
  break;
}
Run Code Online (Sandbox Code Playgroud)