开发者

jQuery if A length == 0 && B length == 1 do something [closed]

开发者 https://www.devze.com 2023-03-12 07:26 出处:网络
开发者_如何转开发 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current for
开发者_如何转开发 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

How do I get this code to work correctly if I want (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'));   

      }
  });


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


try this...

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


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?

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

$('#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:

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

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


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

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() {
    [...]
});
0

精彩评论

暂无评论...
验证码 换一张
取 消