Javascript - transform `elem.style.left` into integer? - Stack Overflow

I need to make this parison:if (x < siteA.style.left || x > siteA.style.left + land.width() ) {ho

I need to make this parison:

if (x < siteA.style.left || x > siteA.style.left + land.width() ) {

however siteA.style.left returns 20px (e.g) so the condition doesn't work. How do I remove the px and transform it into an integer so I can work with it?

I tried alert(parseInt(siteA.style.left)) and it returned NaN however alert(siteA.style.left) returned 30px, 60px etc.

I need to make this parison:

if (x < siteA.style.left || x > siteA.style.left + land.width() ) {

however siteA.style.left returns 20px (e.g) so the condition doesn't work. How do I remove the px and transform it into an integer so I can work with it?

I tried alert(parseInt(siteA.style.left)) and it returned NaN however alert(siteA.style.left) returned 30px, 60px etc.

Share Improve this question asked Sep 3, 2011 at 20:51 lisovaccarolisovaccaro 34k99 gold badges270 silver badges423 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

Use parseInt:

var leftPos = parseInt(siteA.style.left, 10);

where 10 is the base, good practice to always specify it.

Just use parseInt().

Have you tried this?

var left = parseInt(siteA.style.left.replace('px', ''), 10)

When using jQuery you can use $('#siteA').offset().left which returns only the integer value.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742259148a4410579.html

相关推荐

  • Javascript - transform `elem.style.left` into integer? - Stack Overflow

    I need to make this parison:if (x < siteA.style.left || x > siteA.style.left + land.width() ) {ho

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信