最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论