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

JavascriptJquery : Mathematically Divide Two Variables - Stack Overflow

programmeradmin5浏览0评论

Here's my code:

var frameWidth = 400;
var imageWidth = $('#inner-image').css('width');
var numberOfFrames = imageWidth/frameWidth;

How do I make "numberOfFrames" display as a quotient? I.E. process "frameWidth" and "imageWidth" as numbers, rather than objects?

Let me know if I need to explain myself more clearly. Thanks!

Here's my code:

var frameWidth = 400;
var imageWidth = $('#inner-image').css('width');
var numberOfFrames = imageWidth/frameWidth;

How do I make "numberOfFrames" display as a quotient? I.E. process "frameWidth" and "imageWidth" as numbers, rather than objects?

Let me know if I need to explain myself more clearly. Thanks!

Share Improve this question asked Feb 11, 2011 at 1:48 RrryyyaaannnRrryyyaaannn 2,5975 gold badges19 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15

.css('width') is likely returning the value with px. You can use parseInt() to get only the number.

var frameWidth = 400;
var imageWidth = parseInt( $('#inner-image').css('width'), 10);
var numberOfFrames = imageWidth/frameWidth;

The second argument 10 specifies the base that parseInt() should use.

You can also use the width()(docs) method to get the result without the px.

var frameWidth = 400;
var imageWidth = +$('#inner-image').width();
var numberOfFrames = imageWidth/frameWidth;

Here I used the unary + operator to make it a Number instead of a String.

发布评论

评论列表(0)

  1. 暂无评论