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

javascript - jQuery width() function returns wrong value in Internet Explorer 9 - Stack Overflow

programmeradmin0浏览0评论

I used Easy Slider jQuery plugin before and never had problem until now. And the problem is strange. Check out this home page

The page will pop an alert showing two values:

$("#slider").width()

and

$("#slider3").width()

I already set the value for both in css. One is 710px and one is 700px.

If you run in IE9, it shows the default value of $(window).width() instead for both, whatever the window or document width currently is. FF and Chrome returned correctly. Why is that?

I used Easy Slider jQuery plugin before and never had problem until now. And the problem is strange. Check out this home page http://bit.ly/HKiWY6

The page will pop an alert showing two values:

$("#slider").width()

and

$("#slider3").width()

I already set the value for both in css. One is 710px and one is 700px.

If you run in IE9, it shows the default value of $(window).width() instead for both, whatever the window or document width currently is. FF and Chrome returned correctly. Why is that?

Share Improve this question edited Apr 16, 2012 at 8:28 JJJ 33.2k20 gold badges94 silver badges103 bronze badges asked Apr 16, 2012 at 8:13 HP.HP. 19.9k56 gold badges159 silver badges258 bronze badges 1
  • 1 Can you upload the related script code as well? – Starx Commented Apr 16, 2012 at 8:17
Add a comment  | 

2 Answers 2

Reset to default 10

Try the outerWidth, and make sure to wrap it in a windows.ready event listener to make sure all DOM element rendered properly before the width being computed

$("#slider3").outerWidth()

I've had problems with jQuery's width()/height()/offset().top/.left etc in the past when I used them before a certain event fully bubbled. See if setTimeout(function() { alert($('#slider').width()); }, 0); has any effect. It's a cheap nextTick() trick that might be just what you need.

发布评论

评论列表(0)

  1. 暂无评论