Tod*_*ddN 1 jquery if-statement
如果我想要(A.length == 0 && B.length == 1),我该如何让这段代码正常工作?
$(function() {
if (($('.thePrices').find('.addcartforlower').length == 0 ) && ($('.exclusive').length == 1 )) {
$('.thePrices').find('a').after($('.wb_main').addClass('winbuyerset'));
$('.thePrices').find('.wb_main').before($('#compare'));
}
});
Run Code Online (Sandbox Code Playgroud)
提炼
$('.thePrices').find('.addcartforlower').length
Run Code Online (Sandbox Code Playgroud)
变成一个像这样的变量:
var addcartforlowerlength = $('.thePrices').find('.addcartforlower').length;
Run Code Online (Sandbox Code Playgroud)
为另一边做类似的事情。那么您的测试将很简单:
if (addcartforlowerlength === 0 && exclusivelength === 1)
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助