开发者

jQuery: using css selector to compare position values?

开发者 https://www.devze.com 2023-01-12 22:17 出处:网络
Can I use the following code to compare the value of the attribute \"left\" ? if ($(\'.pager\').css(\'left\') < 100 + \'px\')

Can I use the following code to compare the value of the attribute "left" ?

if ($('.pager').css('left') < 100 + 'px')

Or should I different code ? (My 开发者_如何转开发issue is how to deal with 'px', if I should remove it or at least use it consistently in the code.

thanks


Just use parseInt, it will ignore the px for you:

if (parseInt($('.pager').css('left')) < 100)

Or you can use the left property within position():

var left = $('.pager').position().left;
if (left < 100) // Etc


Consider using a variable.

var pagerPos = parseInt($('.pager').css('left'))
if (pagerPos < 100) {...}

Remember that your if statement is in this case trying to compare a number value to a number value. You need to use parseInt() to strip off the 'px' held in the CSS. After that you should be golden!


you can use .style of the DOM element

document.getElementById("id").style.left
0

精彩评论

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