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() {
[...]
});
精彩评论