开发者

IF doesn't work inside each()

开发者 https://www.devze.com 2023-03-16 08:05 出处:网络
any if statement I put inside this function refuses to work. The statement is true for some elements with this class and false for some, however all ignore it

any if statement I put inside this function refuses to work.

The statement is true for some elements with this class and false for some, however all ignore it

(This is very stripped functionality just to locate the problem...)

$(".bought").each(function(index) {
Height = $(this).css("height")
    if (Height > 19) {
        alert(Height);
    }
});

this does work:

开发者_Go百科
$(".bought").each(function(index) {
Height = $(this).css("height")
    alert(Height);
});


$(".bought").each(function(index) {
    thisHeight = $(this).height()
    if (thisHeight > 19) {
        alert(thisHeight);
    }
});

That works! http://jsfiddle.net/NmHb3/


$(this).css("height") returns string...you need to parse this in int before applying comparison..

 Height = parseInt($(this).css("height"),10)
if (Height > 19) {         alert(Height);     }


Height is returned as a string. The comparision is actually "19" > 19 which yields false.

Try converting it to an integer:

if (parseInt(Height) > 19) {
// ...
}


The below code which you have used returns 'px' appended to the result so you can't compare it to '19'

Height = $(this).css("height")

Parse it like this and let us know how you get on :)

parseInt($(this).css('height'), 10);
0

精彩评论

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