jQuery if A length == 0 && B length == 1 做某事

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6297568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:39:20  来源:igfitidea点击:

jQuery if A length == 0 && B length == 1 do something

jqueryif-statement

提问by ToddN

How do I get this code to work correctly if I want (A.length == 0 && B.length == 1)?

如果我想要(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'));   

      }
  });

回答by Adam Dymitruk

extract

提炼

$('.thePrices').find('.addcartforlower').length

into a variable like so:

变成一个像这样的变量:

var addcartforlowerlength = $('.thePrices').find('.addcartforlower').length;

do something similar for the other side. Then your test will be simple:

为另一边做类似的事情。那么您的测试将很简单:

if (addcartforlowerlength === 0 && exclusivelength === 1)

hope this helps

希望这可以帮助

回答by Kevin Mansel

try this...

尝试这个...

if($('.thePrices').find('.addcartforlower').length == 0 && $('.exclusive').length == 1)
{ .... }

回答by tbleckert

Ok so there's nothing wrong with your if statement (but you should in that case use === instead of ==), but I see that you are using before and after, what do you mean by that?

好的,你的 if 语句没有任何问题(但在这种情况下你应该使用 === 而不是 ==),但我看到你在使用之前和之后,你是什么意思?

after() is used to insert something after the element in the dom, like this:

after() 用于在 dom 中的元素之后插入一些内容,如下所示:

$('#header').after('<div id="main" />');

And the same thing for before but before the element. What you probably are looking for are prev() and next(). Or if you want to actually insert content you're almost right, but what you are doing is trying to select an element rather than creating one. Do like this:

在元素之前但在元素之前也是如此。您可能正在寻找的是 prev() 和 next()。或者,如果您想实际插入内容,您几乎是对的,但您所做的是尝试选择一个元素而不是创建一个元素。这样做:

$('.thePrices').find('a').after('<div class="wb_main.winbuyerset />');

But from you information it's impossible to know what you want.

但是从你的信息中不可能知道你想要什么。

回答by Luke Sneeringer

Your actual function declaration syntax is screwed up. The "if" code is correct, although indented poorly:

你的实际函数声明语法搞砸了。“if”代码是正确的,虽然缩进很差:

if (($('.thePrices').find('.addcartforlower').length == 0 ) && ($('.exclusive').length == 1 )) {
    $('.thePrices').find('a').after(jQuery('div[class="wb_main"]:eq(0)').addClass('winbuyerset'));
    $('.thePrices').find('.wb_main').before(jQuery('div[id="compare"]:eq(0)'));
}

To declare a function, use a form like:

要声明函数,请使用如下形式:

var function_name = function() {
    [...]
};

To just run something on page-load, use a form like:

要在页面加载时运行某些内容,请使用如下形式:

$(document).ready(function() {
    [...]
});